For example, given the string aaaa
, I want a regex like /aa/
to match
- 'aa' index 0,1
- 'aa' index 1,2
- 'aa' index 2,3
But it only seems to match 1. and 3. As seen in here: http://rubular.com/r/j9zl5gP4L9
Is this possible?
For example, given the string aaaa
, I want a regex like /aa/
to match
But it only seems to match 1. and 3. As seen in here: http://rubular.com/r/j9zl5gP4L9
Is this possible?
No. The regex "consumes" the characters from the first match before it looks for the next one. So, there's no way to do exactly what you want (find all three substrings, each consisting of aa
).
That said, you can fake it, like this:
/a(?=a)/g
Demo. This will find matches at indexes 0, 1, and 2, but the (?=a)
isn't part of the match; it's just a condition. This means the matches will be only one character long. But that may be enough for you to piece together what you're looking for in additional code.