Solving Dustin Diaz RegEx Brain Teaser Part II

Dustin Diaz of "./with Imagination" blog and Google, wrote a regex challenge. Basically, write a regular expression that wraps any alpha sequence ("\w+") occurring more than a N number of times, with parenthesis. It actually ended up being pretty easy, not requiring look aheads or any more complex than a backreference. Here is my solution:

Example 1: Bookend Function

var bookend = function(text, start) {
    var rx = new RegExp("(\\w+)\\s((\\1\\s){2})((\\1\\s)*\\1)".replace(2, start - 1), "g");
    return text.replace(rx, $1 $2($4));

Here is a good visualization of this regular expression, Regex Visualization. The "Not Yet Implemented" are backreferences to the first wordchar loop.

In other words, the regular expression first matches a group of word characters from the "(\\w+)", this becomes the "\\1" backreference. Next a space is matched "\\s", followed by the matched word characters and a space repeated one less than start "((\\1\\s){2})". The two will be replaced at runtime with the right value, and the outside parenthesis creates the second backreference that we will use in the replace statement. Last we match any number of word characters and space repetitions, ending with a single instance of the word characters "((\\1\\s)*\\1)". This will become the forth backreference used in the replace statement.

Here is a test page so you can play with it. Keep in mind I dont do any parameter checking, so if start is less than 1, it is not going to work.