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
  • 369,052
  • 73
  • 620
  • 1,021
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
  • 43,544
  • 11
  • 104
  • 132
3

Short answer: you can't.

JavaScript's RegExp Object does not support negative lookbehind.

Community
  • 1
  • 1
FK82
  • 4,747
  • 4
  • 26
  • 40
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
  • 16,429
  • 36
  • 107
  • 177