-1

How do I extract the text from link HTML element if the URL matches a particular domain?

E.g. extract hello from:

<a href="https://example.com/2018/11/22/ff/">hello</a>

If the URL wasn't example.com, then it should ignore it.

I'm using regex </?a(|\s+[^>]+)> but it works for all domains when it should only work for example.com.

xlm
  • 6,854
  • 14
  • 53
  • 55

1 Answers1

0

I am a noob developer! But i think this will work for you!

//The website you want to block
var regex = /example.com/g
// Select all anchor text from the window
var textLink = document.querySelectorAll("a");

//Check all anchor from the windows if they contain the "example.com" if they do, the href will be replace with "#"
textLink.forEach(  e => {
    if(regex.test(e.href)){
    e.href = "#";
}});