1

I'm writing this simple regexp in Javascript

str = "1-2-3-456789";
re = /(\d+)/;
found = str.match(re);
alert(found);

I'm waiting for an array with 1,2,3,456789 but the result is 1,1 Why??

1 Answer 1

2

Try adding the g (global) flag to your pattern.

re = /(\d+)/g;

Otherwise it would stop at the first match, in which case the return would be 1 (for the whole match), 1 (the control group).

Sign up to request clarification or add additional context in comments.

1 Comment

As a rule of thumb, when you start making mistakes like these, it's time to hit the bed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.