Match word boundaries except for angle brackets

kemaltuna

New Member
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 \[code\](\\b)(word)(\\b)\[/code\] 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 \[code\]([\\s-_()])(word)([\\s-_()])\[/code\], 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\[code\]var str = "Plain planes are plain. Plain pork is plain. Plain pasta is plainly plain.";str = str.replace(/(\b)(plain)(\b)/, "$1<a href='http://stackoverflow.com/questions/13768758/www.plain.com'>$2</a>$3");// this will result in the first instance of 'plain' becoming // a link to www.plain.comstr = str.replace(/(\b)(plain)(\b)/, "$1<a href='http://stackoverflow.com/questions/13768758/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\[/code\]
 
Back
Top