Javascript 从字符串中删除 HTML(在 JSON 响应中)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11784703/
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 from a string (in JSON response)
提问by John Cooper
var json = {
"Item": {
"Items": {
"Body": "<SPAN style=\"LINE-HEIGHT: 115%; FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 11pt; mso-fareast-font-family: Calibri; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA\">\"We have all been inoculated with Christianity, and are never likely to take it seriously now! You put some of the virus of some dreadful illness into a man's arm, and there is a little itchiness, some scratchiness, a slight discomfort, disagreeable, no doubt, but not the fever of the real disease, the turning and the tossing, and the ebbing strength. And we have all been inoculated with Christianity, more or less. We are on Christ's side, we wish him well, we hope that He will win, and we are even prepared to do something for Him, provided, of course, that He is reasonable, and does not make too much of an upset among our cozy comforts and our customary ways. But there is not the passion of zeal, and the burning enthusiasm, and the eagerness of self-sacrifice, of the real faith that changes character and wins the world.\"<B>A.J. Gossip<\/B><\/SPAN>",
},
}
};
var someString = json.Item.Body.replace(/<br\s*\/?\s*>/g,"\n");
alert(someString);
?
I am getting HTML
tags in my response, which i should remove or parse it. How can i go about this. I did for BR
tags, but now it seems there are other tags too coming in.
我HTML
在我的回复中收到标签,我应该删除或解析它。我该怎么办。我做了BR
标签,但现在似乎还有其他标签进来了。
回答by Segu
I think all you need is a simple regex to strip html style tags from the content. Try this.
我认为您只需要一个简单的正则表达式即可从内容中去除 html 样式标签。尝试这个。
str.replace(/<\/?[^>]+>/gi, '')
回答by G.G.
This should do the magic http://jsfiddle.net/Ygfvp/Anyway it won't strip the html comments
这应该可以发挥神奇作用http://jsfiddle.net/Ygfvp/无论如何它不会剥离 html 评论