0

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"`
1
  • just match the quoted url, not all the bolierplate html... ( [^ '"]+ ) Commented Mar 27, 2015 at 5:06

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

5 Comments

@TabraizAli I know it is a string.
@TabraizAli it is more fail-proof and using regex to parse html is an invitation to Satan.
@TabraizAli if it can be done through DOM go for DOM method as regex solution can fail with slightest change in html.
@AmitJoki how do i select only links inside ss url as mentioned in question
@TabraizAli like my edit. Just use a ternary operator and filter the rest
1
<a href='([^>']*)'[^>]*>ss URL<\/a>

Try this.See demo.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.