开发者

Ruby match part of escaped html string

How do I match the id of this escaped html string? Part of the string starts with id=\" and ends with \" In this example I need art_images_attributes_0_attachement

"<div class=\"input string optional\"><label class=\"string optional\" for=\"art_images_attributes_0_attachement\"> Attachement</label><input class=\"string optional\" id=\"art_images_attributes_0_attachement\" maxlength=\"255\" name=\"art[images_at开发者_如何学Ctributes][0][attachement]\" size=\"50\" type=\"text\" /></div>"
 => "<div class=\"input string optional\"><label class=\"string optional\" for=\"art_images_attributes_0_attachement\"> Attachement</label><input class=\"string optional\" id=\"art_images_attributes_0_attachement\" maxlength=\"255\" name=\"art[images_attributes][0][attachement]\" size=\"50\" type=\"text\" /></div>"

Best regards. Asbjørn Morell


The following regex will capture your desired outputp:

/id="([^"]*)"/

And a Rubular: http://www.rubular.com/r/dtXqK2GBPe

Note that there is not actually a \ character before the quotation marks, in Ruby the content of the string "id=\"test\"" is id="test", the \ is just there to escape any " that should be included.

And so that it will also work with escaped quotes inside of the id value, you can use this:

/id="([^"\\]*(?:\\.[^"\\]*)*)"/

http://www.rubular.com/r/b66eWno8Ce


Regex-wise this would do it:

id=\\\"(.+?)\\\"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜