0

I've read other pages supplying the RegEx, but the post doesn't seem to be working in my tests. I'm just wanting to remove the page=X portion from a url string (/search?page=2&some_var=foo).

When I try this:

urlStr = req.url
urlStr.replace(/&foo(=[^&]*)?|^foo(=[^&]*)?&?/, '')

It doesn't remove the page portion. If I move the page to the end or middle of the string the RegEx elector works.

Anyone have a better Reg Ex solution? I'm only going to have a few query strings so I'm pretty sure I'm not going to run into edge cases.

Community
  • 1
  • 1
AlbertEngelB
  • 16,016
  • 15
  • 66
  • 93

1 Answers1

5
urlStr = urlStr.replace(/\bpage=[^&]+/, '');
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • You know what's funny? The reason I ran into this was because I failed to actually replace urlStr (I just did urlstr.replace()). Haha! – AlbertEngelB Feb 01 '13 at 17:59
  • Perhaps `/\bpage.../` to prevent false matches (e.g. `'multipage=false'`) – JDB Feb 01 '13 at 18:00
  • Basically I just did urlStr.replace(blah blah), not urlStr = urlStr.replace(). Gotta love putting your foot in your mouth! – AlbertEngelB Feb 01 '13 at 18:01