I want to remove an HTML tag from string for example remove div,p,br,...
我想从字符串中删除一个HTML标记,例如删除div,p,br,…
I'm trying to do this:
我试着这么做:
var mystring = "<div><p>this</p><p>is</p><p>my</p><p>text</p><p>sample</p><p> </p><p> </p></div>"
var html3 = $(mystring).text();
but the result is:
但结果是:
"thisismytextsample "
How can do it like : "this is my text sample"
如何做到:“这是我的文本示例”
1
You can use replace function :
你可以使用替换功能:
var mystring="<div><p>this</p><p>is</p><p>my</p><p>text</p></div>"
var stripped = mystring.replace(/(<([^>]+)>)/ig," "); // this is my text
source : http://css-tricks.com/snippets/javascript/strip-html-tags-in-javascript/
来源:http://css-tricks.com/snippets/javascript/strip-html-tags-in-javascript/
4
You can get all p tag text in array and then join them with spaces:
你可以在数组中获取所有的p标签文本,然后将它们与空格连接:
$(mystring).find('p').map(function() {
return $(this).text();
}).toArray().join(' '));
演示工作
1
Try this using regular expression
尝试使用正则表达式
var mystring="<div><p>this</p><p>is</p><p>my</p><p>text</p><p>sample</p><p> </p><p> </p></div>"
function RemoveHTMLTags(string1) {
var regX = /(<([^>]+)>)/ig;
var html = string1;
return html.replace(regX, " ");
}
var res = RemoveHTMLTags(mystring);
alert(res);
演示
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2014/08/12/ecc6e51fb8a057e279bfb95f533cdd00.html。