How to use regexp to not match HTML tags that have certain tags inside them? [duplicate]

a 夏天 提交于 2021-01-20 13:49:09

问题


I have a link like this that I want to match with regexp:

<a href="tel:something">something</a>

I managed to match it with <a[^>]+tel:.*?>.*?<\/a>

But I don't want to match links that have <span></span> nested inside them:

<a href="tel:[some_numbers]"><span class="hello">Hello</span>[some_numbers]</a>

I tried to use negative lookahead (?!(<\/?span[^>]*>)) to exclude <span></span> but it didn't work. My regex below:

/<a[^>]+tel:.*?>(?!(<\/?span[^>]*>)).*?<\/a>/ig

回答1:


You should do this with XPath:

// Our HTML source
var s = `<a href="tel:something">something1</a>
<a href="tel:[some_numbers]"><span class="hello">Hello1</span>[some_numbers]</a>
<a href="tel:something">something2</a>
<a href="tel:[some_numbers]"><span class="hello">Hello2</span>[some_numbers]</a>
<a href="tel:something">something3</a>
<a href="tel:[some_numbers]"><span class="hello">Hello3</span>[some_numbers]</a>`;

// Create a root div because XML requires a single root element
var div = document.createElement('div');

// Set the innerHTML to our string
div.innerHTML = s;

// Find <a> tags with no direct child <span> tag(s)
var iterator = document.evaluate('//a[not(span)]', div, null, XPathResult.ANY_TYPE, null);

// Set the iterator
var thisNode = iterator.iterateNext();

// Loop the iterator and log the node found
while (thisNode) {
  
  console.log(thisNode);
  
  thisNode = iterator.iterateNext();
}

https://jsfiddle.net/kad3ouqL/

This should yield:

<a href="tel:something">something1</a>
<a href="tel:something">something2</a>
<a href="tel:something">something3</a>


来源:https://stackoverflow.com/questions/65289753/how-to-use-regexp-to-not-match-html-tags-that-have-certain-tags-inside-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!