2011-03-11 3 views
0

제발, 모든 양식 태그를 제거하려면 정규식이 필요합니다. 예를 들어 는 HTML 텍스트에 내가있는 경우 :모든 입력/텍스트 영역을 제거하는 정규식/SELECT FROM html

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Title appears in the browser's title bar...</title>  
<style type="text/css"> 
body {background-color:ffffff;background-image:url(http://);background-repeat:no-repeat;background-position:top left;background-attachment:fixed;} 
h1{font-family:Cursive;color:000000;} 
p {font-family:Cursive;font-size:14px;font-style:normal;font-weight:normal;color:000000;}  
</style>  
</head> 
<body> 
<form name="fr"> 
<input name="ss" id="sss" value="as1"> 
</form> 
<h1>Heading goes here...</h1> 
<p>Enter your paragraph text here...</p> 
</html> 

내가 얻을 수있는 모든 입력 태그를 제거해야합니다

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Title appears in the browser's title bar...</title>  
<style type="text/css"> 
body {background-color:ffffff;background-image:url(http://);background-repeat:no-repeat;background-position:top left;background-attachment:fixed;} 
h1{font-family:Cursive;color:000000;} 
p {font-family:Cursive;font-size:14px;font-style:normal;font-weight:normal;color:000000;}  
</style>  
</head> 
<body> 
<form name="fr"> 
</form> 
<h1>Heading goes here...</h1> 
<p>Enter your paragraph text here...</p> 
</html> 
+6

_where의 내 스프레이 bottle_ ... 제외하고는 [정규식 일치 열린 태그의 중복 가능성 XHTML 자체 포함 태그] (http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags). BTW, 정규식 질문을 할 때 프로그래밍 언어를 지정해야합니다. –

+1

"모든 양식 태그를 제거하려면 정규식이 필요합니다"- 아니요, Matt의 링크 –

+0

에 설명 된대로 HTML 파서가 필요합니다. 어떤 정규식을 사용하고 있습니까? – ridgerunner

답변

1

나는 확실하지 정규식 여기에 당신의 최선의 선택입니다.

 
var container = document.getElementById("fr"); 

if (container.hasChildNodes()) 
{ 
    while (container.childNodes.length >= 1) 
    { 
     container.removeChild(getElementsByTagName("input"));  
    } 
} 
0

그 가정 : 1)을 HTML은 W3C 유효성 검사기 (HTML 4.01, XHTML 1.0, 엄격한 또는 전환) 통과를 실행 한 다음, 다음과 같은 자바 스크립트 재질 - 구글 번역 참고 2) 더 <![CDATA[ 섹션, HTML 주석이 없습니다 스크립트, 태그 속성 또는 시퀀스가 ​​포함 된 스타일 : <FORM 또는 </FORM) 3. 짧은 태그가없는 경우 다음 PHP 스크립트가 트릭을 수행해야합니다. (정규식은 많이 주석 처리되어 있음을 유의하십시오. 모든 중요하지 않은 정규식 해야한다!)

<?php // test.php 20110312_0000 
$data = file_get_contents('valid_markup.html'); 

$re = '%# Match an HTML FORM element. 
(     # $1: Opening tag. 
    <FORM\b   # Opening tag opening delimiter and element name. 
    (?:    # Non-capture group for optional attribute(s). 
    \s+    # Attributes must be separated by whitespace. 
    [\w\-.:]+  # Attribute name is required for attr=value pair. 
    (?:    # Non-capture group for optional attribute value. 
     \s*=\s*  # Name and value separated by "=" and optional ws. 
     (?:   # Non-capture group for attrib value alternatives. 
     "[^"]*"  # Double quoted string. 
     | \'[^\']*\' # Single quoted string. 
     | [\w\-.:]+\b # Non-quoted attrib value can be A-Z0-9-._: 
    )    # End of attribute value alternatives. 
    )?    # Attribute value is optional. 
)*     # Allow zero or more attribute=value pairs 
    \s*    # Whitespace is allowed before closing delimiter. 
    >     # Opening tag closing ">" delimiter. 
)     # End $1: Opening tag. 
(     # $2: Tag contents. 
    [^<]*    # Everything up to next tag. (normal*) 
    (?:    # We found a tag (open or close). 
    (?!</?FORM\b) < # Not us? Match the "<". (special) 
    [^<]*   # More of everything up to next tag. (normal*) 
)*     # Unroll-the-loop. (special normal*)* 
)     # End $2. Tag contents. 
(</FORM\s*>)   # $3: Closing tag. 
     %ix'; 
$data = preg_replace($re, '$1$3', $data); 
echo($data); 
?> 

p.s. 여러분 중 누군가가 정규 표현식을 사용하기 전에이 해결책이 부적절하다고 판단한 경우, 이것이 실패 할 수 있음을 보여주는 한 가지 예 (명시된 가정을 충족)를 제공하십시오. 아니면 더 빠른 다른 방법 (정규식 또는 다른 방법)을 보여주십시오. (그리고 새로운 것을 찢어 버리지 말아라. 나는 여기서 새롭고 더 잘 모른다!)

2

정규 표현식은 문맥 자유 문법을 처리 할 수 ​​없다. 임의 HTML을 처리하는 데 사용할 수 없습니다.

일부 간단한 태그 (하위 태그가없는 태그)를 제거하는 데 사용할 수 있습니다. 그러나 중첩 된 태그가 포함 된 html이 발견되면 정규식은 매우 빠르게 실패합니다.

사용자가 식별 한 세 개의 태그 중 두 개 (일반적으로 input, select, textarea)에는 중첩 태그가 없으며 select에는 한 수준의 태그 만 있어야하지만 잘못된 형식의 HTML을 만난다는 것을 결코 보장 할 수는 없습니다. 그 아래에 태그가 있습니다.

짧은 대답은 입력의 올바른 형식에 대해 절대적으로 확신하지 않는 한이 작업에 대해 정규식을 사용하지 않는 것입니다. 잘 구성된 입력의

(즉, 그들은 또한 "<"를 가지고 안 따옴표 안에 ">"문자) :

<input(\s+[^>]*)?>| 
<textarea(\s+[^>]*)?>.*?</textarea(\s+[^>]*)?>| 
<select(\s+[^>]*)?>(<option(\s+[^>]*)?>.*?</option(\s+[^>]*)?>)*</select(\s+[^>]*)?> 
관련 문제