Match pattern except under one condition Regex

Matthew Harwood

I'm trying to match a patterned with regex except when the pattern is escaped.

Test text:

This is AT\&T® is really cool Regex

You can see with my \& I'm manually escaping. And therefore, do not want the regex to match.

Regex:

const str = 'This is AT\&T® is really cool Regex'

str.replace(/\&(.*?)\;/g, '<sup>&$1;</sup>');

Expected output

This is AT&T<sup>&reg;</sup> is really cool Regex

Hard to explain I guess but when the start of this regex looks for a & and ends with a ; however, if & is preceded with at \ like \& than do not match and look for the next \&(.*?)\;

Anton

You can use negative lookbehind This regex works fine with the example /(?<!\\)\&(.*?)\;/g

Edit 1

To workaround in JS you can use [^\\] that will match everything except backslash. The overall regex /[^\\]\&(.*?)\;/g It works for your example.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Regex pattern to match any character except the last one

Regex match all except a pattern

Match everything except my pattern (RegEx)

Regex to match everything except trailing pattern

Regex - how to match everything except a particular pattern

Regex: Match patterns except with pattern preceding

Regex pattern force match with condition and not for another condition

Match all hosts except one, regex

Sed and regex to match all number except as part of another pattern

Regex Pattern to Match, Excluding when... / Except between

Match everything except a complex regex pattern and replace it in Pandas

Match Regex except Regex

CSS regex selector match one OR another condition?

Regex pattern infinite number of times except last one different

Regex match all email except within anchor tag one

Regex to match for all occurrences except when only one exists

Regex to match every occurence of a character except the first one

How to match a regex pattern for multiple files under a directory in perl?

Find regex pattern match string have multiple condition?

Regex to match everything except this regex

sed to replace lines matched by a pattern range if one line match a condition

sub except the first pattern that match

pattern match failure in if condition

Regex - pattern to match text from one street number to the next

Change regex pattern to match string one character at a time

How to match or replace a pattern of two appearances of one statement in RegEx?

Regex Pattern Should Not Match More Than One Sentence

in regex how to match multiple Or conditions but exclude one condition

how to match exactly one word among the others in REGEX 'or'(|) condition