Regular expression for getting HREF from footer

前端 未结 3 1442
梦如初夏
梦如初夏 2021-01-28 05:46

I have a requirement where I need to get the last HREF in the HTML code, means getting the HREF in the footer of the page.

Is there any direct regular expression for the

相关标签:
3条回答
  • 2021-01-28 06:30

    No regex, use the :last jQuery selector instead. demo :

    <a href="foo-link">foo</a>
    <a href="bar-link">bar</a>
    var link = $("a:last");
    
    0 讨论(0)
  • 2021-01-28 06:43

    You could use plain JavaScript for this (if you don't need it to be a jQuery object):

    var links = document.links;
    var lastLink = links[links.length - 1];
    var lastHref = lastLink.href;
    alert(lastHref);
    

    JS Fiddle demo.

    Disclaimer: the above code only works using JavaScript; as HTML itself has no regex, or DOM manipulation, capacity. If you need to use a different technology please leave a comment or edit your question to include the relevant tags.

    0 讨论(0)
  • 2021-01-28 06:44

    It's not a good idea to parse html with regular expressions. Have a look at HtmlParser to parse html.

    0 讨论(0)
提交回复
热议问题