Master JavaScript Regular Expressions
Master JavaScript Regular Expressions
Written by Ian Elliot   
Friday, 28 September 2012
Article Index
Master JavaScript Regular Expressions
Lookahead capture

Lookahead Capture

There are two lookahead captures.

Zero-width positive lookahead assertion


This continues the match only if the regex matches on the immediate right of the current position but doesn’t capture the regex or backtrack if it fails.

For example,


only matches a word ending in a digit but the digit is not included in the match.

That is it matches Paris9 but returns Paris as capture 0. In other words, you can use it to assert a pattern that must follow a matched subexpression.

Zero-width negative lookahead assertion


This works like the positive lookahead assertion but the regex has to fail to match on the immediate right. For example:


only matches a word that doesn’t have a trailing digit.  That is, it matches Paris but not Paris9.


So far we have created regular expressions with the idea that we can use them to test that a string meets a specification or to extract a substring.

These are the two conventional uses of regular expressions. However you can also use them to perform some very complicated string editing and rearrangements.

The whole key to this idea is that you can use the captures as part of the specified replacement string. The only slight problem is that the substitution strings use a slightly different syntax to a regular expression.

The replace method is a String function and it accepts a RegExp object to specify the match :


simply takes every match of the associated regular expression and performs the substitution specified. Notice that it performs the substitution on the first match and the result returned is the entire string with the substitution made.

For example, if we define the regular expression:

var ex1= /(ISBN|ISBN-13)/;

and apply the following replacement:

var ans="ISBN: 978-1871962406".

then the ISBN suffix will be replaced by ISBN-13. Notice that an ISBN-13 suffix will also be replaced by ISBN-13 so making all ISBN strings consistent.

This is easy enough to follow and works well as long as you have defined your regular expression precisely enough.

More sophisticated is the use of capture groups within the substitution string.

You can use:




to refer to capture group n.  There are a range of other substitution strings but these are fairly obvious in use:

  • $$ insert $
  • $& the complete matched string
  • $` the portion of the string before the match
  • $' the portion of the string after the match

As an example of how this all works consider the problem of converting a US format date to a European format date i.e. to change mm/dd/yyyy to dd/mm/yyyy

First we need a regular expression to match the mm/dd/yyyy format:

var ex1=/(\d{1,2})\/(\d{1,2})\/(\d{4})/;

This isn’t a particularly sophisticated regular expression but we have allowed one or two digits for the month and day numbers but insisted on four for the year number. You can write a more interesting and flexible regular expression for use with real data. Notice that we have three capture groups corresponding to month, day and year.

To create a European style date all we have to do assemble the capture groups in the correct order in a substitution string:


This substitutes the day, month and year capture groups in place of the entire matched string, i.e. the original date.

Avoid overuse

Regular expressions are addictive in a way that can ultimately be unproductive.

It isn’t worth spending days crafting a single regular expression that matches all variations on a string when building one or two simpler alternatives and using a wider range of string operations would do the same job as well if not as neatly.

Resist the temptation to write regular expressions that you only just understand and always make sure you test them with strings that go well outside of the range of inputs that you consider correct – greedy matching and backtracking often result in the acceptance of a wider range of strings that was originally intended.

If you take care, however, regular expressions are a very powerful way of processing and transforming text without the need to move to a complete syntax analysis package.

Releated Articles

Finite State Machines


To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin,  or sign up for our weekly newsletter.




blog comments powered by Disqus



jQuery UI and Auto-Complete Address Entry

jQuery UI has a little-known feature that makes interactive auto-complete very easy. In this hands-on tutorial we put it together with the cloud-based Global Express Entry address auto-complete servic [ ... ]

JQuery 3 - Reinventing Events

JavaScript is an event driven language - which means you can't avoid dealing with events. jQuery doesn't just provide a browse-independent way of working with events, it reinvents the event system. Wi [ ... ]

Other Articles

Last Updated ( Friday, 28 September 2012 )

RSS feed of all content
I Programmer - full contents
Copyright © 2017 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.