0

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 same?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
EnthuDeveloper
  • 672
  • 9
  • 25
  • No............. also: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags If you would add more information, like in which form you have the HTML, which is the environment your code is running, which language you are using.... then we might be able to make suggestions. – Felix Kling Oct 20 '11 at 08:32
  • @Bakudan: There is no indication that this question is related to JavaScript or jQuery (and it seems even less likely given the OP's question history). – Felix Kling Oct 20 '11 at 08:47
  • Please don't repost your questions. – Lasse V. Karlsen Oct 20 '11 at 10:20
  • From the following code I want to extract HREF based on span item . What is the regular expression for the same in Java Hjem – EnthuDeveloper Oct 20 '11 at 14:41
  • @ChandraTheCenturion What programming language? There's usually much better ways to parse HTML than regular expressions. – Philipp Reichart Oct 20 '11 at 17:06

3 Answers3

2

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");
Bakudan
  • 19,134
  • 9
  • 53
  • 73
0

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.

David Thomas
  • 249,100
  • 51
  • 377
  • 410
0

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

flash
  • 6,730
  • 7
  • 46
  • 70