0

I'm trying to write a function that matches an array of strings to a string of HTML code. It would then place a bold and /bold around the match. Now if I were matching the strings to regular text, the function would be something like:

function boldText(matches, text)
{
    var pattern = new RegExp(matches.join("|"),"g");
    text.replace(pattern,"<b>"+__what to write here?__+ "</b>"
}

However, since it's an HTML file, I don't want to bold anything that's between a less-than-sign and a greater-than-sign. Only stuff outside of HTML tags should be bolded. What might that regular expression be?

To reiterate, I'm looking for a regular expression that matches if-and-only-if the match isn't between a 'less-than-sign'+ any number of characters and any number of characters + a "greater-than-sign"

Ugh, nobody is answering me :(

  • 1
    Don't parse HTML with regular expressions! – Tibos Nov 20 '13 at 15:09
  • 1
    @Tibos Is there some evidence that `` tag deprecated? – VisioN Nov 20 '13 at 15:11
  • http://stackoverflow.com/questions/271743/whats-the-difference-between-b-and-strong-i-and-em – howderek Nov 20 '13 at 15:13
  • @VisioN thanks for pointing it out. It seems the meaning of `B` elements has changed (rather than deprecated the element): http://www.whatwg.org/specs/web-apps/current-work/multipage/text-level-semantics.html#the-b-element – Tibos Nov 20 '13 at 15:13
  • What's wrong with parsing HTML with regular expressions? – user2946134 Nov 20 '13 at 15:15
  • Isn't this besides the point? I'm trying to learn how to do this with regular expressions at least so I can understand regular expressions better. Please answer. Thanks. – user2946134 Nov 20 '13 at 15:22

1 Answers1

1

You should use the $& pattern as follows:

text.replace(pattern, '<b>$&</b>');
VisioN
  • 143,310
  • 32
  • 282
  • 281