1

I have the following two urls:

url1 = http://localhost:8000/i/dashboard-view?
&level_to_load=1&click_value=%C2%AB%20backll%20Billing%20PartiesC2%
AB%20backll%20Billing%20Parties 

url2 = http://localhost:8000/i/dashboard-view?&level_to_load=1
&click_value=%C2%AB%20backll%20Billing%20PartiesC2%AB%20backll%20Billing%20Parties
&new_value=2

I want to be able to pull out the part that contains "click_value=...".

The resulting two urls after the regular expression should then be:

url1 = http://localhost:8000/i/dashboard-view?&level_to_load=1

url2 = http://localhost:8000/i/dashboard-view?&level_to_load=1&new_value=2

The current regex I have is:

url.replace(/click_value=.+&/, '');

But it is obviously insufficient. What would be the correct regex here?

0

1 Answer 1

2

You can match the click_value GET strings with this regex:

&?click_value=[^&#\s]*

Therefore, to nix them,

result = url.replace(/&?click_value=[^&#\s]*/mg, "");

Discussion

The key here is matching the right number of characters and no more. I revised my original answer after @ridgerunner's insightful observation that in many urls, though maybe not in yours, there can be an anchor tag at the end, starting with #, which we don't want to replace. Thanks to him for the improved answer, which no longer uses lookaheads.

Token-by-Token

&?                       # '&' (optional (matching the most amount
                         # possible))
click_value=             # 'click_value='
[^&#\s]*                 # any character except: '&', '#', whitespace
                         # (\n, \r, \t, \f, and " ") (0 or more times
                         # (matching the most amount possible))
Sign up to request clarification or add additional context in comments.

8 Comments

Note that the query portion of a URL may be followed by a: #fragment. Thus, instead of: .*?(?=&|$), (or better yet: .*?(?=[&#]|$) to handle any fragment), I'd just use: [^&#\s]* which is shorter, faster and pretty much matches the same thing.
@ridgerunner Always a treat to read your regex thoughts... Always juicy... Hope you're traveling well. You are right about potential # fragments in urls. None in his input so I'll leave it as is, but added a segment pointing to an alternate solution in the comments. All the best. :)
Wouldn't /&?click_value=[^&]*/mg be good enough? If not, why?
@pawel Because that also matches &click_value=34 and my brown dog :)
@zx81 just like the first example ;) (I was thinking about processing urls one by one). For urls in a blob of text I'd go with /&?click_value=[^&\s]*/mg : regex101.com/r/yT1pD9
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.