Search code examples
regexnode.jsurlxregexp

How to Match only url from a tag node js


I have a tag <a href="#caveat1"><span style="color: rgb(255,255,255);">[1]</span></a>

I am using this regex <a href="(.*)">(.*)<\/a> But its not parsing the url only. Its also parsing <span style="color: rgb(255,255,255);">[1]</span>

How can i get only the url from a tags?


Solution

  • Easily! Capture everything that is between href, that is a key word, and <.

    href=(.*?)>
    

    If you don't want to capture "", try this one:

    href="(.*?)">
    

    Although I am not much experienced with node.js, I think this one may work, but it won't be hard for you if you know the Regex.

    var pattern = new RegExp(/href="(.*?)">/);
    

    Here is Regex101.