Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there

I am trying to build a JavaScript regex to strip out the & from the key in this example:

c&.a.&activitymap.&page=https%3A%2F%2Fsitdev.online¤cy=eur

if there are any matches it would give this result:

c.a.activitymap.page=https%3A%2F%2Fsitdev.online¤cy=eur

What I have tried:

I have tried a several times to match these results but no luck with this regex:

([^=&]*(?:=[^=&]*&[^=&]*)?)&


Here is a reg ex example, but I need it to match every key/value not just the first 2:

regex101 debugger[^]

Thanks
Posted
Updated 29-Jan-23 22:22pm
v4
Comments
PIEBALDconsult 27-Jan-23 17:24pm    
Which flavor of Regular Expressions? I use only .net's version.
Member 15627495 28-Jan-23 3:17am    
with native functions working on arrays, you have a solution :
begin_string = "the&vars&are&arg" ;
middle_string = begin_string.split('&') ; // all & are the separators, the coming array have each parts isolated ( separator & )
final_string = middle_string.join() ; // a new string is build by concatenation of the 'middle_string' members.

// final_string is "thevarsarearg"



or the string.replace function :

begin_string.replaceAll('&', '');  all '&' are 'empty chars' now

1 solution

How about:
RegEx
(?:([^&]+)&)
Demo[^]
 
Share this answer
 
Comments
David Pell 2023 30-Jan-23 9:52am    
Hi Richard,
Thanks for your post. This solution strips out all the ampersands.
We want to get rid of the ampersands in the key, but not between the key and value:

the end result should look like this:

x=1&a.activitymap.page=https%3A%2F%2Fsitdev.onlineglobal.currency=eur&activitymap.page=https%3A%2F%2Fsitdev.onlineglobal

Let me know if you need more clarification?
Patrice T 30-Jan-23 11:57am    
Improve the question with this information and give sample to illustrate.
Use Improve question to update your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900