繁体   English   中英

允许一些html标签的正则表达式

[英]Regular expression that allow some html tags

我正在使用AngularJS文本编辑器,我想添加更多HTML标记以供文本编辑器接受,而不是编辑器允许的默认HTML标记。

例如:文本编辑器将允许

<p>,<ul>,<li>,<b>,<ol> ,</p>,</ul>,</li>,</b> and </ol>

验证字符串的正则表达式是什么?

请参考以下脚本。 使用下面的函数,您将能够剥离所有HTML标记,但function参数中允许的除外。

<script type="text/javascript">
//Function to strip all the tags except allowed tags
function strip_tags(input, allowed) {
    allowed = (((allowed || '') + '')
    .toLowerCase()
    .match(/<[a-z][a-z0-9]*>/g) || [])
    .join(''); // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
    var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
    commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
    return input.replace(commentsAndPhpTags, '')
    .replace(tags, function($0, $1) {
        return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
    });
}
var str = strip_tags(
   '<p>There is some <u>text</u> here</p>',
   '<p><ul><li><b><ol>' // Allowed tags
);
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM