First, I had to look up the details of the method and how it works. The Mozilla Developer Center has some very clean, straightforward documentation. Once I had that, I set up this test page.
This test page takes some song lyrics and finds the pattern defined as words that modify other words (I had to think of something to test with). Once we have our target text and our pattern, we do a conditional loop until the RegExp::exec() method no longer returns a valid match array. The array that gets returned acts just like the Matcher::Group() method in Java, so this felt very natural:
Running the above page, we get the following output:
BIG modifies BUTTS
........ Phrase: big butts
........ Start Index: 10
........ End Index: 19
........ Index Substring: big butts
BITTY modifies WAIST
........ Phrase: bitty waist
........ Start Index: 113
........ End Index: 124
........ Index Substring: bitty waist
ROUND modifies THING
........ Phrase: round thing
........ Start Index: 134
........ End Index: 145
........ Index Substring: round thing
Anyway, this is very cool stuff. I wish I had known about this earlier. This feels like a very elegant solution, even more so that harnessing the power of the String::replace() method to accomplish the same ends.
Want to use code from this post? Check out the license.