2011-03-11 106 views
0

請,我需要一個正則表達式來刪除所有表單標籤。 例如,如果在HTML文本我有:正則表達式刪除所有輸入/ textarea /選擇從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)。順便說一句,當問正則表達式問題時,你應該指定編程語言。 – 2011-03-11 18:33:54

+1

「我需要一個正則表達式來刪除所有表單標籤」 - 不,你需要一個HTML解析器,正如Matt的鏈接中所解釋的 – 2011-03-12 05:53:54

+0

你正在使用哪種正則表達式? – ridgerunner 2011-03-12 07:20:58

回答

1

我不知道正則表達式是這裏最好的選擇。海外商品會有以下的javascript:

 
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+[^>]*)?>