Reputation: 1169
The URL string may look like:
http://test.com/index.php?page=xxx&item=51
or
http://test.com/index.php?page=xxx&object=22&bla=14
or
http://test.com/index.php?page=xxx
How I would be able to capture word xxx?
Tried something like this:
url_string.match(/\b\?page\=(.*)(\&|$)/)
Upvotes: 0
Views: 50
Reputation: 785098
You can simplify your regex a lot like this:
var matches = url_string.match(/\?page=([^&\n]*)/i);
Upvotes: 3
Reputation: 174706
Usually *
does a greedy match, you need to add a reluctant quantifier ?
just after to *
to do a shortest possible match.
url_string.match(/\?page\=(.*?)(?:\&|$)/gm)
Group index 1 contains the value of page
Upvotes: 1