A regex to remove all input/textarea/SELECT FROM html
Please, i need a regex to remove all form tags . for example if in a html text i have :
<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:fffff开发者_如何学Gof;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>
i need to remove the all input tag to get :
<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>
Regex's cannot handle context-free grammers. It cannot be used to process arbitrary HTML.
You may be able to use it to remove certain simple tags, the ones that do not have sub-tags. However, your regex will fail very rapidly when it encounters html that contains nested tags.
Though two of the three tags you identified (input, select, textarea) typically do not have nested tags, and select should only have one level of tags, you can never guarantee that you won't ever encounter malformed html that just have tags under them.
The short answer is: don't use a regex for this task, unless you are absolutely sure about the well-formedness of the input.
For well-formed inputs (i.e. they also must not have "<" and ">" characters inside quotes):
<input(\s+[^>]*)?>|
<textarea(\s+[^>]*)?>.*?</textarea(\s+[^>]*)?>|
<select(\s+[^>]*)?>(<option(\s+[^>]*)?>.*?</option(\s+[^>]*)?>)*</select(\s+[^>]*)?>
I'm not sure regex is your best option here. Consider the following javascript:
const container = document.querySelector("form[name='fr']")
container.querySelectorAll('input').forEach(el => el.remove())
Assuming that: 1.) the HTML passes the W3C validator (HTML 4.01 or XHTML 1.0, strict or transitional), and: 2.) there are no <![CDATA[
sections, HTML comments, scripts, tag attributes or styles containing the sequence: <FORM
or </FORM
, and 3.) there are no short tags, then the following PHP script should do the trick: (Note that the regex is heavily commented - as all good non-trivial regexes should!)
<?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. Before any of you regexes-aren't-for-parsing purists judge this solution to be inadequate, please provide just one example (which meets the stated assumptions) that demonstrates this can fail. Or show me any other method (regex or otherwise) which is faster. (and please don't rip me a new one - I'm new around here and don't know any better!)
精彩评论