-2

my regex is /<a[\s]+([^>]+)>((?:.(?!\<\/a\>))*.)<\/a>/g to match all <a> tag in string. but i need to match <a> with a special href link. for example : Only <a> tags that have href="https://example.com" links

Atul Mathew
  • 445
  • 3
  • 15
Alireza Hamid
  • 101
  • 1
  • 1
  • 6
  • Is this answer your question:https://stackoverflow.com/questions/48063160/extract-inner-text-from-anchor-tag-string-using-a-regular-expression-in-javascri – Atul Mathew Dec 21 '19 at 07:58

1 Answers1

1

You can use simple DOMParser or do it with a temporary DOM element. And then you can filter a tag using attribute equals selector.

let str = `sdsdsd 
<a href="https://sample.com" >abc</a>
<a href="https://example.com" >abc</a>`

const temp = document.createElement('div');
temp.innerHTML = str;

temp.querySelectorAll('a[href="https://example.com"]').forEach(ele => console.log(ele.outerHTML))

Using regular expressions to parse HTML: why not?

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188