tags:

views:

64

answers:

4

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'

+3  A: 

^ 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
i need to match letters, symbols and numbers. basically i need to match anything that's in between the quotes there.
hatorade
use `[^"]+` to capture symbols
Amarghosh
/<input name="authenticity_token" type="hidden" value="[^"]+" \/>/ returns the entire string as well, not the value i want
hatorade
you are reading match[0] - use grouping and read match[1]
Amarghosh
see my update -
Amarghosh
A: 

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.

soulmerge
A: 

You don't need the ^ and $:

x = response.match(/<input name="example" type="hidden" value=".+" \/>/)[0]
ennuikiller
A: 

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
S.Mark