JavaScript Ninja - No RegEx With Backtrack And Global-Search

Posted at

Just in-case you've ever fiddled around with JavaScript's Regular-Expression modifiers,
and wondered why you might not get the Backtrack..
- a.k.a back-reference,
-- a.k.a group referencing,
--- a.k.a "that thing with the brackets that extracts some $hit from within RegEx.."
..to always work..

You are most likely have *enhanced* your regex with a Global-Search modifier (g),
and that is when all hell broke loose


*in the following example ignore the m(multi-line) modifier..

Global-Search beats Backtrack:
"10 sold hello world 15 sold hello world".match(/(\\d+) sold/mg)

["10 sold", "15 sold"]






No Global-Search? - Backtrack works..
"10 sold hello world 15 sold hello world".match(/(\\d+) sold/m)

["10 sold", "10"]





And here is a nice-little trick that background-loops around the matches: