Reputation: 15476
I want to remove the element tag and want to preserve the text inside the tag. I use the replace function of RegExp but it removed everything: The tag and the text inside the tag.
I dont want that, I want to remove the tag only. Clean up the text, remove the tags, I want to present the text only.
var str = str.replace(/<.>[^.]*\/.>/, "");
I used this, but there's a problem, it also removed the text inside it!
Upvotes: 1
Views: 689
Reputation: 284786
Quick and dirty:
var str = "<foo>bar</foo>"; alert(str.replace(/<[^>]+>/g, ""))
Consider using real parsing and DOM manipulation instead. Correct processing of HTML using RegExp is very difficult.
Upvotes: 0
Reputation: 11729
If you're indeed using jQuery, and want to remove all tags, then simply set the .html() to the .text()
var e = $("#yourelement");
e.html(
e.text()
);
Upvotes: 0