简体   繁体   中英

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

Use single regex with unnecessary capturing group removed.

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

DEMO

You can add several .replace methods in a row.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM