gobler
gobler

Reputation: 143

Is there an alternative for lookbehind in this case?

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.

Upvotes: 1

Views: 67

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626691

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.

Upvotes: 1

Related Questions