0

I'm trying to get all possible matches from a string using regular expression, basically prevent the regex from consuming the characters found in previous match. Example:

'aaaa'.match(/(\w).?\1/g)

would return an array similar to this

result = ['aa', 'aaa', 'aa', 'aaa', 'aa']

where:

result[0] = the first two letters,
result[1] = the first three letter,
result[2] = the second and the third letter, ....

PS: please pardon any misused terms as I'm new to regex.

The match() function used for simplicity, in reality I want to use matchAll() so I can get the match index along with each match.

0 Answers0