0

I have this regular expression:

var a = window.location.href.replace(/((.*)[&|?])(sessionid(=[^&]*))([&|#](.*))/, '$1$5');

That remove the sessionid from an example url like this:

http://...&parent_location=Test&abc=123&sessionid=q26bh6bkm8g49aeopem2obdm87igrsfe&...

The result will be:

http://...&parent_location=Test&abc=123&&...

My question is, if in that replace I can add the replace also for the parent_location

3
  • What is your expected output? Commented Dec 27, 2016 at 10:47
  • 4
    1. Parse the URL/query string into an array/object. 2. Manipulate the data as needed. 3. Reformat into a URL. - Much saner than regexen. Commented Dec 27, 2016 at 10:48
  • This result: http://...&&abc=123&&... I wan't remove also the parent_location query string, would be appreciate remove also the & not needed Commented Dec 27, 2016 at 10:51

2 Answers 2

2

Use single regex with unnecessary capturing group removed.

str.replace(/([&?])(?:sessionid|parent_location)=[^&#]*(?=[&#]|$)/m, '$1')

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

Doesn't work with an ash tag http://...&parent_location=Test&abc=123&sessionid=q26bh6bkm8g49aeopem2obdm87igrsfe#abc/abc should remain: http://...&abc=123#abc/abc
just add # inside the first character class.. ie, [&?#]
0

You can add several .replace methods in a row.

var a = window.location.href
    .replace(/((.*)[&|?])(sessionid(=[^&]*))([&|#](.*))/, '$1$5')
    .replace(/*statement*/);

Comments

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.