Tabraiz Ali
Tabraiz Ali

Reputation: 677

Extract Url from a string though regex

Trying to extract all urls from a string an store it in an array

Using the below regex:

/<a href=([^>]*)>ss URL<\/a>/g

for the string:

 <a href='https://zzzz' target="_blank">ss URL</a>

but i am getting the output with target blank:

`'https://zzzz' target="_blank"`

Upvotes: 0

Views: 86

Answers (2)

Amit Joki
Amit Joki

Reputation: 59232

Just don't. Just use DOM methods to get them. First, we create a temporary div and then set it's inner html to that of the input string. The we can go through the <a> and return their href property using Array.map

var elem = document.createElement('div');
elem.innerHTML = str;
var urls = [].map.call(elem.querySelectorAll('a'), function(a){
    return a.innerText.toLowerCase() == "ss url" ? a.href : "";
}).filter(String);

Upvotes: 2

vks
vks

Reputation: 67968

<a href='([^>']*)'[^>]*>ss URL<\/a>

Try this.See demo.

https://regex101.com/r/sJ9gM7/11

Upvotes: 1

Related Questions