I have an example:
var p = /^(\d)+$/;
var s = '834736';
var a = p.exec(s);
console.log(a);
I want to group the first digit ^(\d), but the result is:
["834736", "6"]
It catch the last digit. Anybody can explain to me? Thanks!
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.
If you want to catch the first digit only, put the first digit in the group, and match the rest of the digits outside of the group, like so:
The reason you only catch the last digit is, that you repeat the same group for each digit you match. This means, every time it matches a new digit, the old contents of the group is replaced, and thus you get the last digit as the result of your group.