views:

174

answers:

3

Hi, 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!

+3  A: 

what about

var obj = $('#element');

var str = obj.text();

alex
A: 

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()
);
great_llama
A: 

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.

Matthew Flaschen