I have this regex
var mregex = /(\$m[\w|\.]+)/g;
string mstring= "$m.x = $m.y";
So basically capture each instance of $m.[+ any number of alphanumeric or . until another character or the end]
I have this working in C# but I’m trying to port it over to javascript, so dropped the name capture.
var match = mregexp.exec(mstring);
match has
0: “$m.x”
1: “$m.x” // not $m.y as I would have expected.
What am i doing wrong?
thanks
You regular expression just matches once. The
[0]element of the return array is the entire matched substring. The[1]element is the first group, which in your case is the same. You’d have to call.exec()again to get it to find the second instance.You can pass a function to
.replace(), which I personally like:That’d show you both matched groups. (The function is passed arguments that are of the same nature as the elements of the returned array from
.exec().)