I am writing a bit of javascript that grabs user ID’s. It works, but the problem is the actual regular expression is being included in the results.
My code it:
var regex = /profile\.php\?id=(\d*)/g;
var matches = source.match(regex);
And it returns:
profile.php?id=1111,1111,profile.php?id=2222,2222,profile.php?id=33333,33333,
All I want is the user ID’s. Am I doing something wrong?
string.match with the g flag only returns strings that match the full regular expression. What you want is capturing groups, you’ll need to use RegExp.exec
Something like this:
You should read up on capturing groups in regex to understand why this works. The first group is always the full match then each of the capturing groups in order.