Regex javascript to match href - Stack Overflow

admin2025-04-06  0

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?

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?

Share Improve this question edited May 8, 2010 at 4:06 user177800 asked May 8, 2010 at 4:05 zx.zx. 2492 gold badges4 silver badges12 bronze badges 7
  • 2 parse it with an HTML parser, parsing HTML with regex will always in end in tears eventually. – user177800 Commented May 8, 2010 at 4:07
  • Will your search strings always follow that same format (<u class="..." href="...)? – ABach Commented May 8, 2010 at 4:11
  • Yes, it will always follow the same format. – zx. Commented May 8, 2010 at 4:13
  • 1 In the spirit of @fuzzy's ment, I refer you to this answer: stackoverflow./questions/1732348/… – Nick Craver Commented May 8, 2010 at 4:14
  • Do you know how I would do this? That's fine if it ends in tears :x – zx. Commented May 8, 2010 at 4:31
 |  Show 2 more ments

3 Answers 3

Reset to default 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.

This should return both h and t values:

logout.php\?h=(\w+)&t=(\w+)
/href="[^"]+"/g
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1743872077a222162.html

最新回复(0)