7

Does anyone know how translate the POSIX regexp (?<!X)A in JS?

Find A only if not preceded by X.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Fabio
  • 71
  • 1
  • 1
    You might find this interesting: http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript. – pimvdb Aug 11 '11 at 18:21
  • I don't think that negative lookbehinds are in POSIX, lookarounds are not supported in BRE nor ERE. ;-) – Qtax Aug 11 '11 at 19:13

3 Answers3

5

Simply check for either the beginning (ergo there is no X) or that there is a non-X character.

(^|[^X])A

For more than one character, you could check for A and then check the matched text for X followed by A, and discard the match if it matches the second pattern.

Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
3

Short answer: you can't.

JavaScript's RegExp Object does not support negative lookbehind.

Community
  • 1
  • 1
FK82
  • 4,907
  • 4
  • 29
  • 42
0

Try this:

var str = "ab"; 
console.log(/a(?!x)/i.exec(str)); //a

var str = "ax"; 
console.log(/a(?!x)/i.exec(str)); //null

if you need of part after "a", try:

   /a(?!x).*/i
The Mask
  • 17,007
  • 37
  • 111
  • 185