Help write regex that will surround certain text with <strong> tags, only if the <strong> tag isn't

Posted by sahil on Stack Overflow See other posts from Stack Overflow or by sahil
Published on 2010-05-17T10:03:19Z Indexed on 2010/05/17 10:10 UTC
Read the original article Hit count: 313

Filed under:
|
|

I have several posts on a website; all these posts are chat conversations of this type:

AD: Hey!
BC: What's up?
AD: Nothing
BC: Okay

They're marked up as simple paragraphs surrounded by <p> tags.

Using the javascript replace function, I want all instances of "AD" in the beginning of a conversation (ie, all instances of "AD" at the starting of a line followed by a ":") to be surrounded by <strong> tags, but only if the instance isn't already surrounded by a <strong> tag.

What regex should I use to accomplish this? Am I trying to do what this advises against?

The code I'm using is like this:

var posts = document.getElementsByClassName('entry-content');

for (var i = 0; i < posts.length; i++) {
    posts[i].innerHTML = posts[i].innerHTML.replace(/some regex here/,
    'replaced content here');
}

© Stack Overflow or respective owner

Related posts about regex

Related posts about JavaScript