Leroy Thompson Leroy Thompson - 3 months ago 8
Javascript Question

How do I match any number after comma using regex?

I'm using this expression capture all numbers.

var regex = /\,([-+]?(\d*[.])?\d*)/g;


But it still captures preceding comma.

How do I get rid of it?


http://regexr.com/3e47t

Answer

But it still captures preceding comma.

No, it matches the preceding comma, but it doesn't capture it:

var regex = /\,([-+]?(\d*[.])?\d*)/g;
var str = "testing,123";
var match = regex.exec(str);
console.log("The match was      : " + match[0]); // ",123"
console.log("But the capture was: " + match[1]); // "123"

Comments