javascript删除html标记,但不删除其中的内容,并且不<;a>;带有正则表达式的标记
如何删除字符串中的所有标记而不是javascript删除html标记,但不删除其中的内容,并且不<;a>;带有正则表达式的标记,javascript,regex,Javascript,Regex,如何删除字符串中的所有标记而不是?而不是里面的文字 例如:BoldGo-here应该是:BoldGo-here您可以删除所有看起来像的字符串,而不是 不要忘记添加一个不区分大小写的修饰符,以避免匹配尝试以下操作: function strip_tags(input, allowed) { allowed = (((allowed || '') + '') .toLowerCase() .match(/<[a-z][a-z0-9]*>/g) || []) .
?而不是里面的文字
例如:
BoldGo-here
应该是:BoldGo-here
您可以删除所有看起来像
的字符串,而不是
不要忘记添加一个不区分大小写的修饰符,以避免匹配尝试以下操作:
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 html = 'some html code';
html = strip_tags(html, '<a>');
功能条标签(输入,允许){
允许=((允许的| |“”)+“”)
.toLowerCase()
.match(//g)| |[])
.join(“”);//确保允许的参数是仅包含小写标记的字符串(您想操作字符串还是DOM?为什么要标记regex?根据第一个问题,您可能不需要它们。您…我添加了可选的\/?
-我想您也想保留结束标记,对吗?我还添加了注释和以前的regex。
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 html = 'some html code';
html = strip_tags(html, '<a>');