0

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'

Tony Stark
  • 24,588
  • 41
  • 96
  • 113

4 Answers4

3

^ 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*\/>/
Amarghosh
  • 58,710
  • 11
  • 92
  • 121
0

Because you have a start-of-line token (^) and an end-of-line token ($) in your regular expression. I think you meant to capture the value, this might solve your problem: value="(.+?)".

Beware, though, that processing html with regular expressions is not a good idea, it can even drive you crazy. Better use an html parser instead.

Community
  • 1
  • 1
soulmerge
  • 73,842
  • 19
  • 118
  • 155
0

You don't need the ^ and $:

x = response.match(/<input name="example" type="hidden" value=".+" \/>/)[0]
ennuikiller
  • 46,381
  • 14
  • 112
  • 137
0

you just need to change [0] to [1]

response='<input name="example" type="hidden" value="matchTextHere" />'

puts response.match(/<input name="example" type="hidden" value="(.*?)" \/>/)[1]

matchTextHere
YOU
  • 120,166
  • 34
  • 186
  • 219