2

Logout

That above is what I want to search for.

I want to get h= and t= from that URL, or just get the entire url in href=""

How would I do this with regex?

zx.
  • 249
  • 2
  • 4
  • 12
  • 2
    parse it with an HTML parser, parsing HTML with regex will always in end in tears eventually. –  May 08 '10 at 04:07
  • Will your search strings always follow that same format ( – ABach May 08 '10 at 04:11
  • Yes, it will always follow the same format. – zx. May 08 '10 at 04:13
  • 1
    In the spirit of @fuzzy's comment, I refer you to this answer: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Nick Craver May 08 '10 at 04:14
  • Do you know how I would do this? That's fine if it ends in tears :x – zx. May 08 '10 at 04:31
  • @zx, "YOURHTMLTEXT".match(/href="[^"]+"/g) – YOU May 08 '10 at 07:26
  • that is actually valid XML using an XML parser would be a better idea than a REGEX, learn from what people are saying and don't do it the wrong way. Use a real parser. –  May 08 '10 at 16:58

3 Answers3

5

You should be able to get the href with:

var array_of_matches = str.match(/href="([^"]*")/g)

Look for 'href="' then start a capture group of all non-double-quote characters, until it ends with a final doublequote. You can pull out the query arguments using more groups inside that group.

Look at this javascript regex tutorial. And the global flag to get the array of matches described in the string regex api.

Stephen
  • 47,994
  • 7
  • 61
  • 70
  • Yeah, that just gets the first url on the page. There are multiple href links – zx. May 08 '10 at 05:03
  • It is valid to keep in mind that this solution will ignore hrefs found between single quotes. To work around that, I would suggest the following: http://regexr.com/3f7q4 – Diego Fortes Feb 06 '17 at 11:12
1

This should return both h and t values:

logout.php\?h=(\w+)&t=(\w+)
Davor Lucic
  • 28,970
  • 8
  • 66
  • 76
0
/href="[^"]+"/g
YOU
  • 120,166
  • 34
  • 186
  • 219