从javascript字符串中删除HTML标记

时间:2021-08-31 17:37:58

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 :

我想从上面的字符串中删除所有的

标签。只是想显示不带html标签的字符串值:

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

10 个解决方案

#1


63  

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();

#2


25  

Here is my solution ,

这是我的解决方案,

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

}

#3


9  

Using plain javascript :

使用纯javascript:

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

#4


4  

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

您可以使用jQuery text()来获得无html标记的纯文本。

Live Demo

现场演示

withoutP = $(content).text()

#5


1  

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

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

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

#6


1  

this one is checking for special chars

这个是检查特殊的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>

#7


0  

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

将隐藏的元素放在标记中,或者从jQuery创建它。使用此代码获取纯文本,不会产生不完整标签的复杂性,<等。

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

#8


0  

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);

#9


0  

Use regex:

使用正则表达式:

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

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

#10


-3  

function htmlTagremove(text) {

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

   return $(cont).text();

}

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

#1


63  

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();

#2


25  

Here is my solution ,

这是我的解决方案,

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

}

#3


9  

Using plain javascript :

使用纯javascript:

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

#4


4  

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

您可以使用jQuery text()来获得无html标记的纯文本。

Live Demo

现场演示

withoutP = $(content).text()

#5


1  

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

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

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

#6


1  

this one is checking for special chars

这个是检查特殊的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>

#7


0  

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

将隐藏的元素放在标记中,或者从jQuery创建它。使用此代码获取纯文本,不会产生不完整标签的复杂性,<等。

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

#8


0  

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);

#9


0  

Use regex:

使用正则表达式:

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

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

#10


-3  

function htmlTagremove(text) {

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

   return $(cont).text();

}

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