I am trying to match the value of the following HTML snippet:
<input name="example" type="hidden" value="matchTextHere" />
with the following:
x = response.match(/<input name="example" type="hidden" value="^.+$" \/>/)[0]
why is this not working? it doesn’t match ‘matchTextHere’
edit:
when i use:
x = response.match(/<input name="example" type="hidden" value="(.+)" \/>/)[0]
it matches the whole html element, and not just the value ‘matchTextHere’
^matches start of a line and$matches end of the line. Change^.+$to\w+and it will work for values that doesn’t contain any symbols. Make it a parenthetical group to capture the value –(\w+)Update: to match anything between the quotes (assuming that there aren’t any quotes in the value), use
[^"]+. If there are escaped quotes in the value, it is a different ballgame..+will work in this case, but it will be slower due to backtracking..+first matches upto the end of the string (because.matches even a"), then looks for a"and fails. Then it comes back one position and looks for a"and fails again – and so on until it finds the"– if there was one more attribute aftervalue, then you will getmatchTextHere" nextAttr="somethingas the match.That being said, the regex will fail if there is an extra space between any of the attribute values. Parsing html with regex is not a good idea – and if you must use regex, you can allow extra spaces using
\s+