Piero
Piero

Reputation: 9273

Replace with regular expression in Javascript

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

Upvotes: 0

Views: 68

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174834

Use single regex with unnecessary capturing group removed.

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

DEMO

Upvotes: 2

Artsiom
Artsiom

Reputation: 1

You can add several .replace methods in a row.

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

Upvotes: 0

Related Questions