Match word boundaries except for angle brackets

264 Views Asked by At

I'm trying to do multiple replacements in a large block of text and convert words into hyperlinks with HTML tags. I find that using the expression (\b)(word)(\b) works most of the time for finding the words I want, but one problem is that angle brackets (< and >) apparently count as boundaries, so when I run the expression again on the same string, I'm matching the words that I had already converted into links. I found a workaround just now in the expression ([\s-_()])(word)([\s-_()]), but this requires me to know what characters are allowed to be around the word rather than disallowing characters. So is there a way I could have an expression that says 'match this word with boundaries except for < and >?

Note - I CANNOT use the global flag. This is meant to be used to do 'n' replacements within a block of text, somewhere between 1 and all.

Ex

var str = "Plain planes are plain.  Plain pork is plain.  Plain pasta is plainly plain.";
str = str.replace(/(\b)(plain)(\b)/, "$1<a href='www.plain.com'>$2</a>$3");
// this will result in the first instance of 'plain' becoming 
// a link to www.plain.com

str = str.replace(/(\b)(plain)(\b)/, "$1<a href='www.plain.com'>$2</a>$3");
// this will NOT make the second instance of 'plain' into 
// a link to www.plain.com
// instead, the 'plain' in the middle of the anchor tag 
// is matched and replaced causing nested anchor tags
1

There are 1 best solutions below

0
IDKFA On

You could try a negative lookbehind like:

(?<!<a href='www\.)(\b)(plain)(\b)