0

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?

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Riess Howder
  • 405
  • 2
  • 7
  • 14
  • 1
    For regex questions like this, it's really helpful to show some sample inputs, the regex you are using (and how you are using it), what you expect to get, and what you are actually getting. With all of that info, it is MUCH easier to help you figure out what's going on. – cdeszaq Apr 21 '11 at 14:31
  • This really is a simple regular expression question. Everything works, that's not the problem. The problem is why is the actual regular expression being returned along with the user ID in the matches array. – Riess Howder Apr 21 '11 at 14:34
  • I was only trying to be helpful. Since I wasn't able to determine exactly what the problem was based on the examples you gave right away, I figured others would be a bit lost also. In general, the `Action`, `Expected`, and `Observed` pattern is one I've found to be the most useful for explaining a problem with a chunk of code. – cdeszaq Apr 21 '11 at 14:40
  • possible duplicate of [How do you access the matched groups in a javascript regex?](http://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regex) – kapa Apr 21 '11 at 14:46

3 Answers3

0

This jsfiddle does what you need:

http://jsfiddle.net/city41/pm3rR/

here is its code:

var source = "profile.php?id=1111,1111,profile.php?id=2222,2222,profile.php?id=33333,33333,";
var regex = /profile\.php\?id=(\d*)/g;
var matches = regex.exec(source);

alert(matches[0]);
alert(matches[1]);
alert(regex.lastIndex);

matches = regex.exec(source);

alert(matches[0]);
alert(matches[1]);
alert(regex.lastIndex);

Whenever you execute regex.exec(...), internall it sets its lastIndex property to the last index of the last match. So if you execute the regex again, it will pick up where it left off last.

And obviously my source string is not the same as yours, but it contains multiple "id=..." in it, so has the same effect.

Matt Greer
  • 60,826
  • 17
  • 123
  • 123
0

the .match() method doesn't just return what you put in parenthesis. It returns the entire matching pattern

Do it like this:

var regex = /profile\.php\?id=(\d*)/g;
var matches = regex.exec(source);

matches[0] will be the entire match but matches[1,2,3 ... n] will be the captured part from the parenthesis.

Cfreak
  • 19,191
  • 6
  • 49
  • 60
  • Is there an alternative method to use? Or is there a conventional solution to my problem? I'm guessing its a common question with Javascript coders. – Riess Howder Apr 21 '11 at 14:36
  • @Riess Please see the link posted under your question for the solution. – kapa Apr 21 '11 at 14:50
0

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:

var text="lots of stuff with something in here a few times so that something comes back multiple times when searching for something.";

var regex=new RegExp("some(thing)","g");

var result=null;

while(result=regex.exec(text)){
    document.write(result[1]);
}

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.

James Montagne
  • 77,516
  • 14
  • 110
  • 130