How to match a keyword on a webpage that is not inside <a> and its href using JavaScript?

I am looking for a page to search for a specific keyword. This in itself is quite simple. An additional complication is that I do not want to match this keyword if it is part of a tag <a>.

eg.

<p>Here is some example content that has a keyword in it. 
I want to match this keyword here but, i don't want to match 
the <a href="http://www.keyword.com">keyword</a> here.</p>

If you look at the example content above, the word "keyword" appears 4 times. I want to combine the first two times with a paragraph, but I do not want to match it when it is displayed as part hrefand as part of the content <a>.

So far I have managed to use this below:

var tester = new RegExp("((?!<a.*?>)("+keyword+")(?!</a>))", 'ig');

The problem with this above is that it still matches the keyword if it is part href.

?

+5
1

JavaScript-. .NET, , lookbehind, JavaScript lookbehind , , , .

, DOM ( , -, JavaScript, ), , <a> ( , ), .

EDIT:

, , . , , , , :

/keyword(?!(?:(?!<a).)*</a)/

?

keyword    # Match "keyword"
(?!        # but only if it is not possible to match the following regex in the text ahead:
 (?:       # - Match...
  (?!<a)   # -- unless it the start of an <a> tag...
  .        # -- any character
 )*        # - any number of times
 </a>      # then match a closing <a> tag. 
)          # End of lookahead assertion.

, . :

  • , </a>
  • <a>.

, <a> , , script, .

+5

All Articles