I want to remove HTML tags from a string. For example assume we have the string:
<p> example ive got a string</P>
How can I write a function that removes the <p><p>
and returns just "example ive got a string"?
This question is related to
jquery
If you want to keep the innerHTML of the element and only strip the outermost tag, you can do this:
$(".contentToStrip").each(function(){
$(this).replaceWith($(this).html());
});
You can use the existing split function
One easy and choppy exemple:
var str = '<p> example ive got a string</P>';
var substr = str.split('<p> ');
// substr[0] contains ""
// substr[1] contains "example ive got a string</P>"
var substr2 = substr [1].split('</p>');
// substr2[0] contains "example ive got a string"
// substr2[1] contains ""
The example is just to show you how the split works.
This is a example for get the url image, escape the p tag from some item.
Try this:
$('#img').attr('src').split('<p>')[1].split('</p>')[0]
Use the .text()
function:
var text = $("<p> example ive got a string</P>").text();
Update: As Brilliand points out below, if the input string does not contain any tags and you are unlucky enough, it might be treated as a CSS selector. So this version is more robust:
var text = $("<div/>").html("<p> example ive got a string</P>").text();
Source: Stackoverflow.com