Seperate a URL variable into two separate variables

Hi,

I have a variable that is passed to a page via URL.
https://www.example.com/?code=12345-122222

What I need to do is take the ‘code’ variable and split it into two separate variables, ‘var_val’ and ‘var_ref’, so the above would be var_val=12345 and var_ref=122222

I get the ‘code’ variable by using Define Query Params on the page, but it’s splitting the ‘code’ variable into the new separate variables which I am struggling with. The Hyphen will also denote the split.

Any help would be great.

Thanks,
Ray

This thread might be helpful.

1 Like

Thank’s Tom, I’ll check it out.

OK, with the link provided by @TomD I manage to figure it out using split(’-’)[0] for the first and split(’-’)[1] for the second.

<dmx-value id="var_val" dmx-bind:value="query.code.split('-')[0] "></dmx-value>

<dmx-value id="var_ref" dmx-bind:value="query.code.split('-')[1] "></dmx-value>

Ray.

Great - I’m glad you found a solution.

Strangely, Wappler is missing some basic text functions/formatters which could offer more powerful parsing options.

I agree.

Something like this should be able to be achieved using functions/formatters.

Maybe this is something the Wappler team can improve on.

Ray.

The split formatter is exactly what you need here.

Split returns an array of values, so that is exactly how it is supposed to access them:

value.split('-')[0] -> first part
value.split('-')[1] -> second part
value.split('-')[2] -> third part
... etc

so the way you did it is the correct way.

Thanks for the confirmation Teodor.

Would be possible to get these functions added to Wappler?

Which one are you missing? Split is already available in the formatters list for strings.

When I do split, it does not give you the option of what part you want to be returned.
image

When I try to edit one that selects the second part I get this error.
image

So it’s like Wappler can’t handle this at the moment?

Aha yes i see what you mean, no the parts are not currently available in the UI.

Would be great if we could. Now that I know how to perform the function without it, I don’t need it, but it would make sense for new and existing users who don’t.

Thanks,
Ray.