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?

7
  • 2
    parse it with an HTML parser, parsing HTML with regex will always in end in tears eventually. Commented May 8, 2010 at 4:07
  • Will your search strings always follow that same format (<u class="..." href="...)? Commented May 8, 2010 at 4:11
  • Yes, it will always follow the same format. Commented May 8, 2010 at 4:13
  • 1
    In the spirit of @fuzzy's comment, I refer you to this answer: stackoverflow.com/questions/1732348/… Commented May 8, 2010 at 4:14
  • Do you know how I would do this? That's fine if it ends in tears :x Commented May 8, 2010 at 4:31

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, that just gets the first url on the page. There are multiple href links
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: regexr.com/3f7q4
1

This should return both h and t values:

logout.php\?h=(\w+)&t=(\w+)

Comments

0
/href="[^"]+"/g

1 Comment

@zx, "YOURHTMLTEXT".match(/href="[^"]+"/g)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.