简体   繁体   English

如何将<a>标签 url 排除到正则表达式?</a>

[英]How to exclude <a> tags url to regex?

The current code is excluding the test link but not the test url.当前代码不包括测试链接,但不包括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此正则表达式用于从内容中搜索单词:test

Example Output: This is a test page.示例 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.问题:如果内容具有此 HTML 标记。 The URL with the word test will be highlighted as well and result to broken tags.带有单词 test 的 URL 也将突出显示,并导致标签损坏。 URL should be excluded to regex. URL 应排除在正则表达式之外。

Example: This is a <a href="https://sample.com/to-test/">test</a>示例:这是一个<a href="https://sample.com/to-test/">test</a>

Please see the link of regex: https://regex101.com/r/aamuTy/3 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排除<a>标记中的文本的一种选择是https://regex101.com/r/aamuTy/4

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

Explanation:解释:

  • First try to match any content within <a>..</a> tag.首先尝试匹配<a>..</a>标记中的任何内容。
  • If the first pattern failed then match with the alternate, \b(estoppel)\b .如果第一个模式失败,则匹配备用模式\b(estoppel)\b This way Group 1 will be available only if the second part of the pattern matches.这样,仅当模式的第二部分匹配时,第 1 组才可用。 We can only work on the values captured in Group1.我们只能处理 Group1 中捕获的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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