Skip to content Skip to sidebar Skip to footer

Removing Html Tags From String

I am trying to remove the HTML tags from a string. Right now I am able to remove the complete HTML tags like for example
dadsasdsad
gives me the

Solution 1:

strippedText[i] = fragments[i]
// full tags
.replace(/<[^>]+>/gm, '')
// partial tags
.replace(/^[^>]+>/gm, '')
.replace(/<[^>]+$/gm, '');

Note that ^ has different meanings: "not" within brackets, "start" outside brackets.

/gm should not be necessary for partial tags, but I left them as I don't know your context and how you're getting partial tags.

Solution 2:

my simple JavaScript library has a function called "strip_tags()" which does the long work for you.

Just say that you have a sentence loaded with HTML formatting tags, and you want to remove them, simply do it like this:

strip_tags("<p>This <em>sentence</em> contains <strong>a lot</strong> of tags!</p>");

This will output "This sentence contains a lot of tags!" (tested on the documentation website).

To read more about this function, please read the documentation at http://docs.funcjs.webege.com/strip_tags().html and if possible, leave feedback through the Feedback Form on the website.

Hope this helps you and anyone else with the same problem! :)

Solution 3:

Using javascript you can do this:

functionremoveHTMLTags(htmlString) {
    if(!htmlString) { return; }
    var mydiv = document.createElement("div");
    mydiv.innerHTML = htmlString;
    return mydiv.textContent || mydiv.innerText || '';
}

[Source]

Post a Comment for "Removing Html Tags From String"