^
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 after value
, then you will get matchTextHere" nextAttr="something
as the match.
x = response.match(/<input name="example" type="hidden" value="([^"]+)" \/>/)[1]
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+
/<input\s+name="example"\s+type="hidden"\s+value="([^"]+)"\s*\/>/