Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS regexp (?<!X)A

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

Find A only if not preceded by X.

like image 666
Fabio Avatar asked Aug 11 '11 18:08

Fabio


People also ask

What is ?! In regex?

Definition and Usage. The ?! n quantifier matches any string that is not followed by a specific string n. Tip: Use the ?= n quantifier to match any string that IS followed by a specific string n.

What does * do in regex?

The Match-zero-or-more Operator ( * ) This operator repeats the smallest possible preceding regular expression as many times as necessary (including zero) to match the pattern. `*' represents this operator. For example, `o*' matches any string made up of zero or more `o' s.

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.

What is RegExp in JS?

RegExp ObjectA regular expression is a pattern of characters. The pattern is used to do pattern-matching "search-and-replace" functions on text. In JavaScript, a RegExp Object is a pattern with Properties and Methods.


2 Answers

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.

like image 89
Platinum Azure Avatar answered Oct 05 '22 21:10

Platinum Azure


Short answer: you can't.

JavaScript's RegExp Object does not support negative lookbehind.

  • similar question
  • another one
like image 39
FK82 Avatar answered Oct 05 '22 23:10

FK82