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.

1

1 Answer 1

5
urlStr = urlStr.replace(/\bpage=[^&]+/, '');
Sign up to request clarification or add additional context in comments.

3 Comments

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!
Perhaps /\bpage.../ to prevent false matches (e.g. 'multipage=false')
Basically I just did urlStr.replace(blah blah), not urlStr = urlStr.replace(). Gotta love putting your foot in your mouth!

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.