簡體   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