I'm struggling with a finding or writing a regex to solve my problem. Here is my example:
$(function () {
// localStorage.clear()
$('#divEditable').focusout(function () {
//var content = $(this).text()
var content = $(this).html().replace(/<(?!br\s*\/?)[^>]+>/g, '');
localStorage.setItem('div', content)
loadDiv()
})
})
var loadDiv = function() {
$('#divEditable').html(localStorage.getItem('div'))
console.log(localStorage.getItem('div'))
}
#divEditable {
width: 400px;
height: 600px;
background: grey;
overflow: auto;
margin-right: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="divEditable" contenteditable></div>
If I paste or write the text with a break:
Nam luctus aliquet nunc, in dignissim neque consequat facilisis. Nullam porta congue diam sit amet accumsan. Mauris vel vulputate lacus.
Maecenas enim dolor, feugiat sed eros ac, blandit vestibulum nibh. Duis volutpat, magna ut semper fermentum, dolor nulla dignissim quam, quis placerat augue leo ut ipsum.
the break will be removed. When I type the text something like this:
1. Test list
2. Test list
3. Test list
The breaks and spaces will be also removed. I'm looking for stripping all text styling, divs, images, lists etc. however only keep any form of text spacing and text breaks no mater if the text is pasted or typed. I hope this make sense.
A plain text + paragraphs, br, space
The working example is also here https://jsfiddle.net/zcwr7v2g/
Thanks