I have the following code :
var str = "4 shnitzel,5 ducks";
var rgx = new RegExp("[0-9]+","g");
console.log( rgx.exec(str) );
The output on chrome and firefox is ["4"].
Why don’t I get the result ["4","5"]?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
execdoes only search for the next match. You need to call it multiple times to get all matches:You can do this to find all matches with
exec:Or you simply use the
matchmethod on the string `str:By the way: Using the RegExp literal syntax
/…/is probably more convenient:/[0-9]+/g.