I want to remove the string code=hads1328fas& on my URL, so that
BEFORE
http://example.com/main/index.php?code=hads1328fas&store=food#home
AFTER
http://example.com/main/index.php?store=food#home
However, the string code=hads1328fas& may not always the same, so the code below won't works in this case.
var url = window.location.href;
url.replace('code=hads1328fas&')
Is there any way that is possible for the case?
Thanks
asked Jun 26, 2012 at 13:56
Charles Yeung
38.8k33 gold badges93 silver badges132 bronze badges
1 Answer 1
Use regular expressions:
url = "http://example.com/main/index.php?code=hads1328fas&store=food#home";
url = url.replace(/code=[^&]+&/,'');
After this, url will contain
http://example.com/main/index.php?store=food#home
answered Jun 26, 2012 at 14:01
kba
19.5k6 gold badges65 silver badges90 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js
"http://example.com/main/index.php?code=hads1328fas&store=food#home".replace(/code=[^&]*/, '')Should do