Javascript 如何从字符串中删除所有html标签

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

How to remove all html tags from a string

javascriptjquery

提问by Suresh Pattu

Hi I am trying to remove all the html tags from a particular string its showing error.

嗨,我正在尝试从显示错误的特定字符串中删除所有 html 标签。

Here is my string:

这是我的字符串:

<p>Hi there</p> ~ wifi free <p>this is test</p> ~ breakfast free <p>This is another test</p>

My jQuery code is here:

我的 jQuery 代码在这里:

var item = <p>Hi there</p> ~ wifi free <p>this is test</p> ~ breakfast free <p>This is another test</p>;
item = item.replace(/~/g, '');
item = item.replace(/<p>/g, '');
item = item.replace('</p>'/g, '');
var splitArray = item.split('<br />');
var l = splitArray.length;
for (var i = 0; i < l; i++) {
    out = out + "<li><span class='sp_icon sp_star_icon'></span> "
          + splitArray[i].trim() + "</li>";
}
console.log(item);

回答by plexus

You can strip out all the html-tags with a regular expression: /<(.|\n)*?>/g

您可以使用正则表达式去除所有 html 标签:/<(.|\n)*?>/g

Described in detail here: http://www.pagecolumn.com/tool/all_about_html_tags.htm

这里有详细描述:http: //www.pagecolumn.com/tool/all_about_html_tags.htm

In your JS-Code it would look like this:

在您的 JS 代码中,它看起来像这样:

item = item.replace(/<(.|\n)*?>/g, '');

回答by James

Don't do it yourself, let the DOM do it for you.

不要自己做,让 DOM 为你做。

E.g. (with jQuery)

例如(使用 jQuery)

jQuery("<p>Hi there</p>...").text();
    // => "Hi there..."

E.g. (without jQuery)

例如(没有 jQuery)

var d = document.createElement('div');
d.innerHTML = "<p>Hi there</p>...";
(d.textContent || d.innerText); // => "Hi there..."

回答by Alexander T.

With vanilla JS you can do it like this

使用 vanilla JS,您可以这样做

var item = '<p>Hi there</p> ~ wifi free <p>this is test</p> ~ breakfast free <p>This is another test</p>';

function getText(html) {
    var tmp = document.createElement('div');
    tmp.innerHTML = html;
    
    return tmp.textContent || tmp.innerText;
}

console.log(getText(item));

回答by michael.zech

var item = '<p>Hi there</p> ~ wifi free <p>this is test</p> ~ breakfast free <p>This is another test</p>'

item = item.replace(/<\/?.+?>/ig, '');

回答by Anil Namde

I hope your are only trying to remove HTML markup from the string. Following should work. Though might need to test.

我希望您只是想从字符串中删除 HTML 标记。以下应该有效。虽然可能需要测试。

filtered = yourString.replace(/<[a-z]{1}>.*?<\/[a-z]{1}>/gi, ""); 

In case you just wanted to get rid of and markup and keep the text inside it then

如果您只是想摆脱和标记并将文本保留在其中,那么

filtered = yourString.replace(/<\/{0,1}[a-z]+>/gi, "");

回答by Nishant Kango

You can use jQuery's text method.

您可以使用 jQuery 的 text 方法。

var item = '<p>Hi there</p> ~ wifi free <p>this is test</p> ~ breakfast free <p>This is another test</p>';
console.log($(item).text());

You can check fiddle code at http://jsfiddle.net/gL7fufax/

您可以在http://jsfiddle.net/gL7fufax/检查小提琴代码

回答by dakab

As asked for your particular string (to remove <p>elements):

根据您的特定字符串(删除<p>元素)的要求:

item = item.replace(/<\/?p>/g,''); // will globally find “<p>” and “</p>” only

回答by NaveenG

You can wrap your string in a jQuery object:

您可以将字符串包装在 jQuery 对象中:

var removeElements = function(text, selector) {
    var wrapped = $("<div>" + text + "</div>");
    wrapped.find(selector).remove();
    return wrapped.html();
}

var removedPString = removeElements("<p>Hi there</p> ~ wifi free <p>this is test</p> ~ breakfast free <p>This is another test</p>", "p");