Fabio
Fabio

Reputation: 71

JS regexp (?<!X)A

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

Find A only if not preceded by X.

Upvotes: 7

Views: 269

Answers (3)

FK82
FK82

Reputation: 5075

Short answer: you can't.

JavaScript's RegExp Object does not support negative lookbehind.

Upvotes: 3

The Mask
The Mask

Reputation: 17427

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

Upvotes: 0

Platinum Azure
Platinum Azure

Reputation: 46193

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.

Upvotes: 5

Related Questions