简体   繁体   中英

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> :

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.

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:

 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> 

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