Home > Software design >  Is there an alternative for lookbehind in this case?
Is there an alternative for lookbehind in this case?

Time:07-17

I have a regex that is not working on Safari because it contains lookbehind which is not supported:

\b((a[a-c])[^\"]*(?<![0-9])2(?![0-9])[^\"]*)

is there any alternative that does EXACTLY what the regex above does? (the character right before the 2 must NOT be a digit so this will pass aa4a2 but not this aa4a22

Thanks.

CodePudding user response:

You can use

\b((a[a-c])(?:[^\"]*[^\"0-9])?2(?![0-9])[^\"]*)

The (?:[^\"]*[^\"0-9])? part is a non-capturing group matching one or zero occurrences of any zero or more chars other than " and then any one char that is not a " and a digit.

Note that the consuming character of the added group is fine here since the [^\"]* pattern was able to match an empty string, hence the use of the optional non-capturing group.

  • Related