简体   繁体   中英

How to exclude <a> tags url to regex?

The current code is excluding the test link but not the test url.

Please see my current code here:

var regexp = new RegExp( "\\b(" +  data.title + ")\\b(?![^<a]*>|[^<>]*</a>)"  , 'i');

This regex is used by searching words from the content: test

Example Output: This is a test page. The word 'test' will be highlighted and will be linked somewhere base on my dictionary.

Issue: If the content have this HTML tag. The URL with the word test will be highlighted as well and result to broken tags. URL should be excluded to regex.

Example: This is a <a href="https://sample.com/to-test/">test</a>

Please see the link of regex: https://regex101.com/r/aamuTy/3

One option to exclude the text in <a> tag is https://regex101.com/r/aamuTy/4

Pattern: <a.*?</a>|\b(estoppel)\b

Explanation:

  • First try to match any content within <a>..</a> tag.
  • If the first pattern failed then match with the alternate, \b(estoppel)\b . This way Group 1 will be available only if the second part of the pattern matches. We can only work on the values captured in Group1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM