简体   繁体   English

javascript regexp:查找包含标记内的字符串或标记内的锚

[英]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,只需检查对象的innerHTMLhref属性中字符串是否存在:

 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.

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