removing html tags from string

前端 未结 3 1098
死守一世寂寞
死守一世寂寞 2021-01-29 07:24

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
相关标签:
3条回答
  • 2021-01-29 07:42

    Using javascript you can do this:

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

    [Source]

    0 讨论(0)
  • 2021-01-29 07:44
    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.

    0 讨论(0)
  • 2021-01-29 07:46

    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! :)

    0 讨论(0)
提交回复
热议问题