从字符串中删除html标签 [英] removing html tags from string

查看:188
本文介绍了从字符串中删除html标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从字符串中移除HTML标签。现在,我可以删除完整的HTML标记,例如< div class =test> dadsasdsad< / div> 给出输出 dadsasdsad



但我无法删除部分标签,如 class =test> dadsasdsad< ; / div> 测试< div class =



使用的是

  strippedText [i] = fragments [i] 
.replace(/<(?:。| \\\
)*?> / gm,'')
.replace(replaceAT,'< span style = font-weight:800>')
.replace(replaceET,'<跨度>');

这里 fragments [i] 包含输入< div class =test> dadsasdsad< / div> ;

解决方案

  strippedText [i] = fragments [i] 
//完整标记
.replace(/< [>] +> / gm ,'')
//部分标签
.replace(/ ^ [^>] +> / gm,'')
.replace(/< [^>] + $ / gm,'');

注意^有不同的含义:括号内的not,括号外的start p>

/ gm不应该是部分标签所必需的,但是我留下了它们,因为我不知道您的上下文以及如何获得部分标签。


I am trying to remove the HTML tags from a string. Right now I am able to remove the complete HTML tags like for example <div class="test">dadsasdsad</div> gives me the output dadsasdsad.

But I'm unable to remove partial tags like class="test">dadsasdsad</div> or testing<div class=

The regular expression that Ive used is

strippedText[i] = fragments[i]
                   .replace(/<(?:.|\n)*?>/gm, '')
                   .replace(replaceAT, '<span style=font-weight:800>')
                   .replace(replaceET, '</span>');

Here fragments[i] contains the input <div class="test">dadsasdsad</div>;

解决方案

strippedText[i] = fragments[i]
// full tags
.replace(/<[^>]+>/gm, '')
// partial tags
.replace(/^[^>]+>/gm, '')
.replace(/<[^>]+$/gm, '');

Note that ^ has different meanings: "not" within brackets, "start" outside brackets.

/gm should not be necessary for partial tags, but I left them as I don't know your context and how you're getting partial tags.

这篇关于从字符串中删除html标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆