How to combine multiple matches (/ g) with backlinks in javascript regex match

I got confused in the array returned by the regex when using both / g (to get multiple matches) and parentheses (to get backlinks). I don’t understand how to get backlinks, because the index array of matches seems to refer to multiple matches, not backlinks.

eg:

string = "@abc @bcd @cde";    
re2 = '@([a-z]+)';    
p = new RegExp(re2,["g"]);    
m = string.match(p)   
for (var i in m) { alert(m[i]; }

it returns "@abc", "@bcd", "@cde"
but I want it to return"abc", "bcd", "cde"

how do i get the latest?

+2
source share
2 answers
var str = "@abc @bcd @cde",
    re = /@([a-z]+)/g,
    match;

while (match = re.exec(str)) {
  // match[1] contains text matched by first group, match[2] - second, etc.
  alert(match[1]);
}
+5
source

You should use a group without capture:

(?:@)([a-z]+)
0
source

All Articles