User_111
User_111

Reputation: 49

Find word in link regex

Is it possible to match the link which contain specific text using regex? I need to add some text after href value. I tried to match "word" in following link:

<a href="http://word/sth">some link</a>

I tried this, but without success:

(https?:\/\/[^word"]+)

Upvotes: 0

Views: 54

Answers (2)

The fourth bird
The fourth bird

Reputation: 163217

If it is an option you could use the DOM instead of a regex:

var div = document.createElement('div');
div.innerHTML = '<a href="http://word/sth">some link</a>';
if (div.firstChild.innerHTML.indexOf("some") !== -1) {
  console.log("contains some!");
  div.firstChild.href += " added text";
}
console.log(div.firstChild);

Upvotes: 0

SCouto
SCouto

Reputation: 7928

You don't actually need ^ and $ Also the brackets are to match a single character in the given set.

Try just:

https?:\/\/(word)

Helpful resource: You can test your regex here

Upvotes: 1

Related Questions