-1

I have a regex that matches a single character x but not xx.

(?<!x)x(?!x)

This works great, the problem is the regex engine in firefox doesn't support look-behinds. (this bug)

Is there any way to rewrite this so it doesn't use a looking behind?

edit:

the solution in the duplicate link doesn't work in this case. If I replace the negative look-behind with

(?!^x)x(?!x)

It doesn't match correctly.

marathon
  • 7,881
  • 17
  • 74
  • 137

2 Answers2

1

Here is a workaround:

^(?:^|[^x])x(?!x)

Demo

It matches x if preceded by beginning of line or by a non-x

Toto
  • 89,455
  • 62
  • 89
  • 125
1

You could use non-capturing groups:

(?:[^x]|^)(x)(?:[^x]|$)

This means we search single "x" symbol in some string. Yes, the regex matches three symbols, but we can refer to match x as $1.

AlexL
  • 355
  • 3
  • 15