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.

Safari Regex Issue with $0-9 In Replacement Text

On a financial site, such as, there are many opportunities to use regular expressions to replace numbers and currency. While replacing numbers with regular expressions will work fine, replacing US currency will cause issues that you should be aware of. The issues arise when using the "String.prototype.replace" method, because a dollar sign ($) followed by a number is a back-reference to a captured parenthesis in the regular expression. Meaning that when the replacement ...

Escaping RegExp Characters Function

If you use regular expressions on your site, then I am sure you have encountered situations where you use a String value, possibly returned from the some user input, as part of your regular expressions. The syntax you might use:

Example 1: Typical Syntax

var haystack = Hello World, my name is NAME!; var needle = NAME; var rx = new RegExp(needle); alert(haystack.replace(rx, Matt Snider)); 
This very simple example shows how you create ...