使用 javascript 删除除 <br> 或 <br/> 标签之外的 html 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16708158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove html tags except <br> or <br/> tags with javascript
提问by cp100
I want to remove all the html tags except <br>
or <br/>
tags from a string using javascript.
I have seen many questions like this but their answers will remove all the html tags including <br>
and <br/>
tags.
我想使用javascript从字符串中删除除<br>
或<br/>
标签之外的所有html标签。我见过很多这样的问题,但他们的回答会删除所有的 html 标签,包括<br>
和<br/>
标签。
Does anyone knows a regex to do this?
有谁知道一个正则表达式来做到这一点?
采纳答案by Sudz
Try This
试试这个
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>");
}
回答by h2ooooooo
Use a negative lookahead (by using a regex such as /<(?!br\s*\/?)[^>]+>/g
):
使用负前瞻(通过使用正则表达式,例如/<(?!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
回答by Sandro Rosa
I've worked on the last suggestion to develop a function removing all or just keeping some tags
我已经研究了最后一个建议,以开发一个删除全部或仅保留一些标签的功能
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>
回答by Mario Brazil - Sage IE
To expand h2ooooooo Answer to include leading spaces and be case insenctive you could use
要扩展 h2oooooooo Answer 以包含前导空格并且不区分大小写,您可以使用
/<(?!\s*br\s*\/?)[^>]+>/gi