最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

jquery - How to remove html tags using JavaScript and keep newline? - Stack Overflow

matteradmin5PV0评论

I want to remove rich text from my contenteditable div keep only newline or br tags when paste something from clipboard.

I'm trying but its doesn't work /

<div id="editableDiv" contentEditable="true" style="min-height: 200px; border: solid 1px #0099FF;"></div>

<input type="button" value="remove" onclick="strip();">

<script>
function strip() {
    var mydiv = document.getElementById("editableDiv");
    var str  = mydiv.innerHTML;
    str = str.replace(/<br>/gi, "\n");
    str = str.replace(/<(?:.|\s)*?>/g, "");
    mydiv.innerHTML = str;
}
</script>

I copy the text from this page /
I know it doesn't have any <br> tag the text. but how can i do like Twitter does like this? Thanks.

I want to remove rich text from my contenteditable div keep only newline or br tags when paste something from clipboard.

I'm trying but its doesn't work http://jsfiddle/marco83/zkf5jkqu/

<div id="editableDiv" contentEditable="true" style="min-height: 200px; border: solid 1px #0099FF;"></div>

<input type="button" value="remove" onclick="strip();">

<script>
function strip() {
    var mydiv = document.getElementById("editableDiv");
    var str  = mydiv.innerHTML;
    str = str.replace(/<br>/gi, "\n");
    str = str.replace(/<(?:.|\s)*?>/g, "");
    mydiv.innerHTML = str;
}
</script>

I copy the text from this page http://www.jacklmoore./autosize/
I know it doesn't have any <br> tag the text. but how can i do like Twitter does like this? Thanks.

Share Improve this question asked Nov 15, 2015 at 17:09 MarcusMarcus 6272 gold badges13 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Try This

<script>
    function strip() {
        var mydiv = document.getElementById("editableDiv");
        var str = mydiv.innerHTML;
        mydiv.innerHTML = remove_tags(str);
    }


    function remove_tags(html) {
        html = html.replace(/<br>/g, "$br$");
        html = html.replace(/(?:\r\n|\r|\n)/g, '$br$');
        var tmp = document.createElement("DIV");
        tmp.innerHTML = html;
        html = tmp.textContent || tmp.innerText;
        html = html.replace(/\$br\$/g, "<br>");
        return html;
    }
</script>

Example Here

So what i am doing is, i am replace <br> and \n with a placeholder $br$ so as to preserve them and then i am removing all the HTML tags from the string and replacing $br$ back with <br>

Post a comment

comment list (0)

  1. No comments so far