<p>测试strip_tags</p>
<br />
<a
name=”strip_tags”
id=”strip_tags”/>
<a
href=”http://yuenshui.com/”>野马</a>
<!– 看不到 –>
<script>
/*
* 过滤字符串中的html标签,此段代码前需要有Array.prototype.search的定义代码
* @param allow 允许使用的标签,如:<a><h2><h3><br>,可以缺省
* @return string 返回过滤后的字符串
* 如:html.strip_tags(‘<b>’);
* 将html中除了<b>和</b>的html标签移除
*/
String.prototype.strip_tags = function (allow) {
var allow = allow ? allow.toLowerCase() : ”;
return this.replace(/<[\/\!\?]?([\w_-]*)[^>]*>/igm , function($0, $1) {
return allow.indexOf(‘<’ + $1.toLowerCase() + ‘>’) > -1 ? $0 : ”;
});
}
alert(document.documentElement.innerHTML);
alert(document.documentElement.innerHTML.strip_tags());
</script>
<p>测试strip_tags</p> <br /> <a name=”strip_tags” id=”strip_tags”/> <a href=”http://yuenshui.com/”>野马</a> <!– 看不到 –> <script> /* * 过滤字符串中的html标签,此段代码前需要有Array.prototype.search的定义代码 * @param allow 允许使用的标签,如:<a><h2><h3><br>,可以缺省 * @return string 返回过滤后的字符串 * 如:html.strip_tags(‘<b>’); * 将html中除了<b>和</b>的html标签移除 */ String.prototype.strip_tags = function (allow) { var allow = allow ? allow.toLowerCase() : ”; return this.replace(/<[\/\!\?]?([\w_-]*)[^>]*>/igm , function($0, $1) { return allow.indexOf(‘<’ + $1.toLowerCase() + ‘>’) > -1 ? $0 : ”; }); } alert(document.documentElement.innerHTML); alert(document.documentElement.innerHTML.strip_tags()); </script>
五月 26th, 2012 at 9:48 下午 #站长社区网
正用得上,好东西