从 javascript 字符串中删除 HTML 标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13911681/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:06:32  来源:igfitidea点击:

Remove HTML tags from a javascript string

javascriptjquery

提问by krrr25

I have this code :

我有这个代码:

var content = "<p>Dear sms,</p><p>This is a test notification for push message from center II.</p>";

and I want to remove all <p>and </p>tags from the above string. Just want to display the string values without html tags like :

我想从上面的字符串中删除所有<p></p>标签。只想显示没有 html 标签的字符串值,例如:

"Dear sms, This is a test notification for push message from center II."

回答by Blender

Why not just let jQuery do it?

为什么不让 jQuery 来做呢?

var content = "<p>Dear sms,</p><p>This is a test notification for push message from center II.</p>";

var text = $(content).text();

回答by Human Being

Here is my solution ,

这是我的解决方案,

function removeTags(){
    var txt = document.getElementById('myString').value;
    var rex = /(<([^>]+)>)/ig;
    alert(txt.replace(rex , ""));

}

回答by Samuel Caillerie

Using plain javascript :

使用普通的 javascript :

content = content.replace(/(<p>|<\/p>)/g, "");

回答by Adil

You can use jQuery text()to get plain text withouthtml tags.

您可以使用 jQuery text()来获取纯文本withouthtml 标签。

Live Demo

现场演示

withoutP = $(content).text()

回答by Alexandru Aliu

var content = "a<br />";
var withoutP = $(content).text()
alert(withoutP )

This one does not work for the .text() solution.

这个不适用于 .text() 解决方案。

回答by Benn

this one is checking for special chars

这是检查特殊字符

var $string = '&lt;a href=&quot;link&quot;&gt;aaa&lt;/a&gt;';
var $string2 = '<a href="link">aaa</a>';
var $string3 = 'BBBBB';
var $string4 = 'partial<script';
var $string5 = 'has    spaces';

function StripTags(string) {

  var decoded_string = $("<div/>").html(string).text();
  return $("<div/>").html(decoded_string).text();

}

console.log(StripTags($string));
console.log(StripTags($string2));
console.log(StripTags($string3));
console.log(StripTags($string4));
console.log(StripTags($string5));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

回答by Cyb

Place hidden elementin markup, or create it from jQuery. Use this code to get plain text without complications with incomplete tags, &lt;etc.

放置hidden element在标记中,或从 jQuery 创建它。使用此代码可获取纯文本,而不会出现标签不完整&lt;等问题。

content = $(hiddenElement).html($(hiddenElement).html(content).text()).text();

回答by Aravind Cheekkallur

Above approch is not working for me. so i got one alternative solution to solve this issue`

以上方法对我不起作用。所以我得到了一种替代解决方案来解决这个问题`

var regex = "/<(.|\n)*?>/";
var originaltext = "1. Males and females were compared in terms of time    management ability. <br><br>The independent variables were the people in the   study.<br><br> Is this statement correct";
var resultText = body.replace(regex, "");
console.log(result);

回答by Steven Koch

Use regex:

使用正则表达式:

var cleanContent = content.replace(/(<([^>]+)>)/ig,"");

var cleanContent = content.replace(/(<([^>]+)>)/ig,"");

回答by karayakar

function htmlTagremove(text) {

   var cont = document.createElement('div'),
   cont.innerHTML=text;

   return $(cont).text();

}

htmlTagremove('<p><html>test');