How can I remove the tag and keep the text in the element using RegExp?

I want to remove an element tag and keep the text inside the tag. I am using the replace function RegExp, but it removed everything: the tag and the text inside the tag.

I don't want this, I only want to remove the tag. Clear text, remove tags, I only want to present text.

var str = str.replace(/<.>[^.]*\/.>/, "");

      

I used this but there is a problem, it also removed the text inside it.

+1


a source to share


3 answers


What about

var obj = $('#element');



var str = obj.text();

+3


a source


If you are really using jQuery and want to remove all tags, just set .html () to .text ()



var e = $("#yourelement");
e.html(
    e.text()
);

      

0


a source


Fast and Dirty:

var str = "<foo>bar</foo>"; alert(str.replace(/<[^>]+>/g, ""))

      

Consider using real DOM parsing and manipulation instead. Correctly processing HTML with RegExp is very tricky.

0


a source







All Articles