使用javascript删除除了<br>或<br/>标签之外的html标签

cp1*_*100 19 html javascript stripping

我想使用javascript 删除除字符串之外的所有html标签<br><br/>标签.我见过很多这样的问题,但他们的答案将删除所有的HTML标签,包括<br><br/>标签.

有谁知道正则表达式这样做?

h2o*_*ooo 44

使用否定前瞻(使用正则表达式/<(?!br\s*\/?)[^>]+>/g):

var html = 'this is my <b>string</b> and it\'s pretty cool<br />isn\'t it?<br>Yep, it is. <strong>More HTML tags</strong>';
html = html.replace(/<(?!br\s*\/?)[^>]+>/g, '');

console.log(html); 
//this is my string and it's pretty cool<br />isn't it?<br>Yep, it is. More HTML tags
Run Code Online (Sandbox Code Playgroud)

演示

  • ⚠ 不要使用此解决方案来清理用户输入:它不会转义诸如 `&lt;br script="whatever"&gt;` 之类的内容...这对 BR 来说还不错,因为它们没有“onload”、“onclick”或此类挂钩执行JS,但是如果适配到其他元素可能会很危险。 (3认同)

Sud*_*udz 12

试试这个

 function remove_tags(html)
 {
   var html = html.replace("<br>","||br||");  
   var tmp = document.createElement("DIV");
   tmp.innerHTML = html;
   html = tmp.textContent||tmp.innerText;
   return html.replace("||br||","<br>");  
 }
Run Code Online (Sandbox Code Playgroud)


xte*_*ore 8

这是一个古老但仍然很重要的问题,所以我想我会提供一个更通用的 ES6 解决方案。

此解决方案将删除除排除标签之外的所有标签,并简化这些标签以删除属性。

如果您想要处理粘贴事件并简化 HTML,这尤其有用。

它还会删除 HTML 注释,因为有时会包含复制/粘贴<!--StartFragment-->等。

  function strip_tags(html, ...args) {
    return html.replace(/<(\/?)(\w+)[^>]*\/?>/g, (_, endMark, tag) => {
      return args.includes(tag) ? '<' + endMark + tag + '>' :'';
    }).replace(/<!--.*?-->/g, '');
  }
Run Code Online (Sandbox Code Playgroud)

它不是构建复杂的,而是进行替换,然后检查标签,返回简单的开始/结束标签或空字符串。

使用示例:

// Strip all except basic formatting and paragraphs and breaks.
const h = strip_tags(html, 'b', 'i', 'u', 'p', 'br');
Run Code Online (Sandbox Code Playgroud)

这就是我用来处理简单 HTML 编辑器的粘贴事件的方法。它并不完美,因为它不能处理奇怪的情况,例如嵌入标签属性中的“>”,但这似乎不太可能发生。

希望它对某人有用。欢迎改进!


San*_*osa 6

我已经研究了最后一个建议,即开发一个删除所有功能或仅保留一些标签的功能

function strip_tags( _html /*you can put each single tag per argument*/ )
{
    var _tags = [], _tag = "" ;
    for( var _a = 1 ; _a < arguments.length ; _a++ )
    {
        _tag = arguments[_a].replace( /<|>/g, '' ).trim() ;
        if ( arguments[_a].length > 0 ) _tags.push( _tag, "/"+_tag );
    }

    if ( !( typeof _html == "string" ) && !( _html instanceof String ) ) return "" ;
    else if ( _tags.length == 0 ) return _html.replace( /<(\s*\/?)[^>]+>/g, "" ) ;
    else
    {
        var _re = new RegExp( "<(?!("+_tags.join("|")+")\s*\/?)[^>]+>", "g" );
        return _html.replace( _re, '' );
    }
}

var _html = "<b>Just</b> some <i>tags</i> and text to test <u>this code</u>" ;
document.write( "This is the original html code including some tags<br>" );
document.write( _html + "<br><br>" ); // original html code
document.write( "Now we remove all tags (plain text)<br>" );
document.write( strip_tags( _html ) + "<br><br>" ); // remove all tags
document.write( "Only the bold tag is kept<br>" );
document.write( strip_tags( _html, "b" ) + "<br><br>" ); // keep <b> only
document.write( "Only the underline tag is kept<br>" );
document.write( strip_tags( _html, "u" ) + "<br><br>" ); // keep <u> only
document.write( "Only the italic tag is kept<br>" );
document.write( strip_tags( _html, "<i>" ) + "<br><br>" ); // keep <i> only
document.write( "Keeping both italic and underline<br>" );
document.write( strip_tags( _html, "i", "u" ) ); // keep both <i> and <u>
Run Code Online (Sandbox Code Playgroud)