How can I Strip all regular html tags except , (>

前端 未结 4 562
误落风尘
误落风尘 2020-12-21 02:29

When a user create a message there is a multibox and this multibox is connected to a design panel which lets users change fonts, color, size etc.. When the message is submit

4条回答
  •  遥遥无期
    2020-12-21 02:59

    I think it would be better to extract to good tags. It is easy to match a few tags than to remove the rest of the element and all html possibilities. Try something like this, I tested it and it works fine:

    // the following regex matches the good tags with attrinutes an inner content
    var ptt = new  RegExp("<(?:img|a|br){1}.*/?>(?:(?:.|\n)*)?", "g");
    var input = "";              
    var result = "";
    
    var match = ptt.exec(input);                
    while (match) {
        result += match;
        match = ptt.exec(input);
    }
    
    // result will contain the clean HTML with only the good tags
    console.log(result);
    

提交回复
热议问题