0

I am trying to extract a ur link from a data item using below regex function but getting error "match not found'.

Content: <a href="https:// ... ">

Regex Function:

export function GetImage2(content) {
    myRegexp = new RegExp(/<a.*?href="(.*?)"/);
    match = myRegexp.exec(content);

    if (match) {
        // match[1] = match[1].split(/\s+/);
        // match[1] = match[1].join(" \' ");
        // console.log('Matches found:' + ' ' + match[1]);
        return match[1];
    }
    console.log('No match found');    
}

I am at wits end on this. Thanks as you assist.

André Kool
  • 4,880
  • 12
  • 34
  • 44
Obisi7
  • 485
  • 1
  • 5
  • 18
  • 1
    Could you add better examples of what your input is/can be and what you expect to match? – André Kool Feb 20 '18 at 15:40
  • 2
    This can use some cleanup, but does work as intended. Most probably, your input is not what you're expecting it to be. – georg Feb 20 '18 at 15:40
  • 2
    Note that by default `.` will not include newlines. If your anchor tag has attributes on new lines, this regex will not find a match. If you need to include newlines in your match, consider using `[\s\S]*?` as an alternative. – JDB Feb 20 '18 at 15:43
  • [H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) - use a parser, but try `]* href="[^"]*"` – ctwheels Feb 20 '18 at 16:12
  • This is the data item I am trying to get just the https:// ... link from this content: "Hotel Fusion" – Obisi7 Feb 20 '18 at 20:34

1 Answers1

-1

You can using this regex to select link from a tag

/([^"]*)">/g

Your code:

export function GetImage2(content) {
    myRegexp = new RegExp(/([^"]*)">/g);
    match = myRegexp.exec(content);

    return match ? match : 0;    
}
anhvietcr
  • 170
  • 2
  • 7
  • getting match of 0 rettunned (ie no match). Trying to extract the https:// tag from " data item – Obisi7 Feb 20 '18 at 21:28
  • The suggested regex works. But I now found out that my content is being returned as "undefined" and hence no match when calling the function to extract from it. Thx – Obisi7 Feb 21 '18 at 18:50