Javascript: negative lookbehind equivalent?

  1. Home
  2. javascript
  3. Javascript: negative lookbehind equivalent?

Is there a way to achieve the equivalent of a negative lookbehind in javascript regular expressions? I need to match a string that does not start with a specific set of characters.

It seems I am unable to find a regex that does this without failing if the matched part is found at the beginning of the string. Negative lookbehinds seem to be the only answer, but javascript doesn’t have one.

EDIT:
This is the regex that I would like to work, but it doesn’t:

(?<!([abcdefg]))m

So it would match the ‘m’ in ‘jim’ or ‘m’, but not ‘jam’

First answer

Lookbehind Assertions got accepted into the ECMAScript specification in 2018. This has been implemented in V8 and shipped without flags with Google Chrome v62 and in Node.js v6 behind a flag and v9 without a flag. So, if you’re developing for a Chrome-only environment (such as Electron), or Node, you can start using lookbehinds today!

Positive lookbehind usage:

console.log(
  "$9.99  €8.47".match(/(?<=\$)\d+(\.\d*)?/) // Matches "9.99"
);

Negative lookbehind usage:

console.log(
  "$9.99  €8.47".match(/(?<!\$)\d+(?:\.\d*)/) // Matches "8.47"
);

Support on other platforms:

  • Mozilla Firefox is working on it: Tracked here.
  • Microsoft Edge is working on it too: Tracked here (user voice suggestion).

Second answer

Let’s suppose you want to find all int not preceded by unsigned:

With support for negative look-behind:

(?<!unsigned )int

Without support for negative look-behind:

((?!unsigned ).{9}|^.{0,8})int

Basically idea is to grab n preceding characters and exclude match with negative look-ahead, but also match the cases where there’s no preceeding n characters. (where n is length of look-behind).

So the regex in question:

(?<!([abcdefg]))m

would translate to:

((?!([abcdefg])).|^)m

You might need to play with capturing groups to find exact spot of the string that interests you or you want to replace specific part with something else.

Third answer

Mijoja’s strategy works for your specific case but not in general:

js>newString = "Fall ball bill balll llama".replace(/(ba)?ll/g,
   function($0,$1){ return $1?$0:"[match]";});
Fa[match] ball bi[match] balll [match]ama

Here’s an example where the goal is to match a double-l but not if it is preceded by “ba”. Note the word “balll” — true lookbehind should have suppressed the first 2 l’s but matched the 2nd pair. But by matching the first 2 l’s and then ignoring that match as a false positive, the regexp engine proceeds from the end of that match, and ignores any characters within the false positive.

Reprint:https://stackoverflow.com/questions/641407/javascript-negative-lookbehind-equivalent
Spread the love

Related articles

Comments are closed.