Regex to allow only set of HTML Tags and Attributes

后端 未结 4 986
一个人的身影
一个人的身影 2020-12-17 05:00

How to allow only specific set of HTML tags & specific set of Attributes using general Regex?

Allowed HTML Tags:

p|body|b

4条回答
  •  执笔经年
    2020-12-17 05:34

    Finally I have achieved this in two steps:-

    //Allowed list of HTML Tags
    
    <(?!/?(p|body|b|u|em|strong|ul|ol|li|h1|h2|h3|h4|h5|h6|hr|a|br|img|tr|td|table|tbody|label|div|sup|sub|caption)(>|\s))[^<]+?>
    
    //Allowed list of HTML Attributes
    
    \s(?!(alt|href|tcmuri|title|height|width|align|valign|rowspan|colspan|src|summary|class|id|name|title|target|nowrap|scope|axis|cellpadding|cellspacing|dir|lang|rel))\w+(\s*=\s*["|']?[/.,#?\w\s:;-]+["|']?)
    

    Using above two regex, I have filtered my whole html.

    EDIT:

    Now I have reduced it into one regex, which filter all required HTML tags & attributes

    (<(?!/?(p|body|b|u|em|strong|ul|ol|li|h1|h2|h3|h4|h5|h6|hr|a|br|img|tr|td|table|tbody|label|div|sup|sub|caption)(>|\s))[^<]+?>)|(\s(?!(alt|href|tcmuri|title|height|width|align|valign|rowspan|colspan|src|summary|class|id|name|title|target|nowrap|scope|axis|cellpadding|cellspacing|dir|lang|rel)\b)[\w:]+(\s*=\s*["|']?[/.,#?\w\s:;-]+["|']?))
    

提交回复
热议问题