28

I have this HTML:

"This is simple html text <span class='simple'>simple simple text text</span> text"

I need to match only words that are outside any HTML tag. I mean if I want to match “simple” and “text” I should get the results only from “This is simple html text” and the last part “text”—the result will be “simple” 1 match, “text” 2 matches. Could anyone help me with this? I’m using jQuery.

var pattern = new RegExp("(\\b" + value + "\\b)", 'gi');

if (pattern.test(text)) {
    text = text.replace(pattern, "<span class='notranslate'>$1</span>");
}
  • value is the word I want to match (in this case “simple”)
  • text is "This is simple html text <span class='simple'>simple simple text text</span> text"

I need to wrap all selected words (in this example it is “simple”) with <span>. But I want to wrap only words that are outside any HTML tags. The result of this example should be

This is <span class='notranslate'>simple</span> html <span class='notranslate'>text</span> <span class='simple'>simple simple text text</span> <span class='notranslate'>text</span>

I do not want replace any text inside

<span class='simple'>simple simple text text</span>

It should be the same as before replacement.

2
  • Could you mention the language you're using and the desired result of the replacement in your question please?
    – Jerry
    Sep 4, 2013 at 19:30
  • @Jerry, The example was added.
    – Sanya530
    Sep 4, 2013 at 19:38

2 Answers 2

88

Okay, try using this regex:

(text|simple)(?![^<]*>|[^<>]*</)

Example worked on regex101.

Breakdown:

(         # Open capture group
  text    # Match 'text'
|         # Or
  simple  # Match 'simple'
)         # End capture group
(?!       # Negative lookahead start (will cause match to fail if contents match)
  [^<]*   # Any number of non-'<' characters
  >       # A > character
|         # Or
  [^<>]*  # Any number of non-'<' and non-'>' characters
  </      # The characters < and /
)         # End negative lookahead.

The negative lookahead will prevent a match if text or simple is between html tags.

17
  • 2
    Yeees! Thank you! Thank you! Thank you! You are my hero! :)
    – Sanya530
    Sep 4, 2013 at 20:01
  • 3
    @Sanya530 :( Maybe try escaping the forward slash (text|simple)(?![^<]*>|[^<>]*<\/)? I don't know on what engine IE rolls on, but if it's similar to Javascript, that should do the trick.
    – Jerry
    Sep 4, 2013 at 20:17
  • 1
    Sorry! It was my mistake in JS. Everything is working PERFECT in IE8. Thank you!
    – Sanya530
    Sep 4, 2013 at 20:45
  • It took me two days to figure this out. Thank you very much, sir! Feb 11, 2015 at 14:44
  • 1
    @A.Seddighi You would have to use a pattern for that specific <a> tag in the negative lookahead
    – Jerry
    Dec 15, 2021 at 12:42
1
^([^<]*)<\w+.*/\w+>([^<]*)$

However this is a very naive expression. It would be better to use a DOM parser.

1
  • Thank you! And if I want to get all 'simple' matches outside the tags how this example will look like? The reason I asking is I need to get the Regex pattern and then replace all matches of 'simple' word outside the tags.
    – Sanya530
    Sep 4, 2013 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.