[英]javascript regexp: find anchors containing string inside tag OR inside tag
I have the following which checks for string str inside the text portion of an anchor, ie <a href="#">text containing str</a>
: 我有以下代码检查锚文本部分(即
<a href="#">text containing str</a>
:
RegExp('<a.*?>.*?str.*?</a>', 'gi')
However, I need it to check inside the tag as well, ie for <a href="link_containing_str">some text</a>
as well. 但是,我还需要它同时检查标签内部,即也要
<a href="link_containing_str">some text</a>
。
I tried the following but it does not work: 我尝试了以下操作,但不起作用:
RegExp('<a.*?str.*?</a>', 'gi')
What syntax do I need to use for this? 我需要使用什么语法?
Instead of using Regex, just check for the existence of the string in the innerHTML
or href
properties on the object: 不用使用Regex,只需检查对象的
innerHTML
或href
属性中字符串是否存在:
document.querySelectorAll('a').forEach(x => { console.log(x.innerHTML.includes('str') || x.href.indexOf('str') != -1); })
<a href="#">text containing str</a> <a href="link_containing_str">some text</a> <a href="#">text</a> <a href="link">some text</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.