2011-03-16 4 views
0

이스케이프 처리 된 html 문자열의 ID는 어떻게 일치합니까? 문자열의 일부는 내가 art_images_attributes_0_attachement에게Ruby가 이스케이프 처리 된 html 문자열의 일부와 일치합니다.

"<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>" 
=> "<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>" 

안부를 필요로 ID = \ 시작 "및 \로 끝나는"이 예에서. Asbjørn Morell는

답변

1

다음 정규식 캡처됩니다 outputp 원하는 :

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

그리고 Rubular 나 : http://www.rubular.com/r/dtXqK2GBPe

참고가 인용 부호 앞에 실제로는 \ 문자가 아니며, Ruby의 문자열 내용은입니다.은 id="test"이고 \은 포함되어야하는 "을 피할 수 있습니다.

그리고 그것은 또한 id 값의 내부 탈출 따옴표와 함께 작동합니다, 당신은이를 사용할 수 있습니다

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

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

+0

고마워요. 정확히 내가 찾던 곳 이었어. string.match (/ id = "([^"] *) "/) [1] – atmorell

0

정규식 현명한이 그것을 할 것입니다 :

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

닉 앤드류의 대답으로 설명하고있는 바와 같이, 당신의 예는 – atmorell

+0

nil을 반환합니다. 내 (순진한) 대답은 당신의 질문에 정확히 일치해야한다는 생각에 기반을 두었습니다. 나는 그 \ s가 무시되어야한다는 것을 알지 못했습니다. – Nick

관련 문제