If I run this:
'121'.match(/[0-9]{2}/gi)
I get back an array with a single result:
['12']
How can I get it to return all results, even if they overlap? I want the result to be this:
['12', '21']
EDIT: Or a better example would be:
'1234567'.match(...);
should give me an array with
[12,
23,
34,
45,
56,
67]
this just won’t work in the way you want.
when you specify pattern
[0-9]{2},match()looks up first occurrence of two digit number, then continues search from that place on. as string length is 3, obviously it won’t find another match.you should use different algorithm for finding all two digit numbers. I would suggest using combination of your first match and do one more with following regex
/[0-9]([0-9]{2})/and combine sets of both first and second run.