Alberto
Alberto

Reputation: 2982

Unexpected result using very simple regexp in javascript

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??

Upvotes: 1

Views: 48

Answers (1)

Madara's Ghost
Madara's Ghost

Reputation: 175098

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).

Upvotes: 2

Related Questions