Please support this book: buy it or donate

9. RegExp lookbehind assertions



This chapter explains the proposal “RegExp Lookbehind Assertions” by Gorkem Yakin, Nozomu Katō, Daniel Ehrenberg.

A lookaround assertion is a construct inside a regular expression that specifies what the surroundings of the current location must look like, but has no other effect. It is also called a zero-width assertion.

The only lookaround assertion currently supported by JavaScript is the lookahead assertion, which matches what follows the current location. This chapter describes a proposal for a lookbehind assertion, which matches what precedes the current location.

9.1. Lookahead assertions

A lookahead assertion inside a regular expression means: whatever comes next must match the assertion, but nothing else happens. That is, nothing is captured and the assertion doesn’t contribute to the overall matched string.

Take, for example, the following regular expression

  1. const RE_AS_BS = /aa(?=bb)/;

It matches the string 'aabb', but the overall matched string does not include the b’s:

  1. const match1 = RE_AS_BS.exec('aabb');
  2. console.log(match1[0]); // 'aa'

Furthermore, it does not match a string that doesn’t have two b’s:

  1. const match2 = RE_AS_BS.exec('aab');
  2. console.log(match2); // null

A negative lookahead assertion means that what comes next must not match the assertion. For example:

  1. > const RE_AS_NO_BS = /aa(?!bb)/;
  2. > RE_AS_NO_BS.test('aabb')
  3. false
  4. > RE_AS_NO_BS.test('aab')
  5. true
  6. > RE_AS_NO_BS.test('aac')
  7. true

9.2. Lookbehind assertions

Lookbehind assertions work like lookahead assertions, but in the opposite direction.

9.2.1. Positive lookbehind assertions

For a positive lookbehind assertion, the text preceding the current location must match the assertion (but nothing else happens).

  1. const RE_DOLLAR_PREFIX = /(?<=\$)foo/g;
  2. '$foo %foo foo'.replace(RE_DOLLAR_PREFIX, 'bar');
  3. // '$bar %foo foo'

As you can see, 'foo' is only replaced if it is preceded by a dollar sign. You can also see that the dollar sign is not part of the total match, because the latter is completely replaced by 'bar'.

Achieving the same result without a lookbehind assertion is less elegant:

  1. const RE_DOLLAR_PREFIX = /(\$)foo/g;
  2. '$foo %foo foo'.replace(RE_DOLLAR_PREFIX, '$1bar');
  3. // '$bar %foo foo'

And this approach doesn’t work if the prefix should be part of the previous match:

  1. > 'a1ba2ba3b'.match(/(?<=b)a.b/g)
  2. [ 'a2b', 'a3b' ]

9.2.2. Negative lookbehind assertions

A negative lookbehind assertion only matches if the current location is not preceded by the assertion, but has no other effect. For example:

  1. const RE_NO_DOLLAR_PREFIX = /(?<!\$)foo/g;
  2. '$foo %foo foo'.replace(RE_NO_DOLLAR_PREFIX, 'bar');
  3. // '$foo %bar bar'

There is no simple (general) way to achieve the same result without a lookbehind assertion.

9.3. Conclusions

Lookahead assertions make most sense at the end of regular expressions. Lookbehind assertions make most sense at the beginning of regular expressions.

The use cases for lookaround assertions are:

  • replace()
  • match() (especially if the regular expression has the flag /g)
  • split() (note the space at the beginning of ' b,c'):
  1. > 'a, b,c'.split(/,(?= )/)
  2. [ 'a', ' b,c' ]

Other than those use cases, you can just as well make the assertion a real part of the regular expression.

9.4. Further reading