In following code:
"a sasas b".match(/sas/g) //returns ["sas"]
The string actually include two sas strings, a [sas]as b and a sa[sas] b.
How can I modify RegEx to match both?
Another example:
"aaaa".match(/aa/g); //actually include [aa]aa,a[aa]a,aa[aa]
Please consider the issue in general not just above instances.
A pure RexEx solution is preferred.
This significantly-improved answer owes itself to @EliGassert.
@EliGassert points out that there is no need to walk through the entire string character by character; instead we can find a match anywhere (i.e. do without the anchor), and then continue one character after the index of the found match. While researching how to retrieve said index, I found that the
re.lastIndexproperty, used byexecto keep track of where it should continue its search, is in fact settable! This works rather nicely with what we intend to do.The only bit needing further explanation might be the beginning. In the absence of the
gflag,execmay never returnnull(always returning its one match, if it exists), thus possibly going into an infinite loop. Since, however,match_overlapby design seeks multiple matches, we can safely recompile any non-globalRegExpas a globalRegExp, importing theiandmoptions as well if set.Here is a new jsFiddle: http://jsfiddle.net/acheong87/h5MR5/.
Output: