2016-10-10 6 views
0

私はAngularJSテキストエディタを使用しています。HTMLタグをデフォルトのHTML以外のテキストエディタで受け入れるように追加します。 EXのために一部のhtmlタグを許可する正規表現

テキストエディタは、文字列を検証するための正規表現である

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

何ができますか?

+0

正規表現はこの仕事のツールではありません。 HTML浄化器が必要です。 – Brad

+0

これに使用したテキストエディタのライブラリリンクを追加してください。 –

+0

2つのテキストエディタライブラリtextAngularとMediumエディタを使用しています –

答えて

0

以下のスクリプトを参照してください。以下の関数を使用すると、関数の引数で許可されている以外のすべてのHTMLタグを削除できます。

<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> 
関連する問題