Given this editable DIV:
<div contenteditable>
First line
Second line
Third line
Fourth line
</div>
Live demo: http://jsfiddle.net/Mpvyk/1/
Notice the 3 empty lines between “Third line” and “Fourth line”. I would like to replace those 3 empty lines with a single empty line.
So, inside the DIV, for all occurrences of this:
'\n' (zero or more spaces or tabs) '\n' (zero or more spaces or tabs) '\n' etc.
replace it with one single empty line:
`\n\n`
What I have so far:
var text = div.textContent;
text.replace(/ ??? /gm, '\n\n');
div.textContent = text;
As you can see, I don’t know how to write the regular expression which would select those occurrences of multiple empty lines.
First, I think the regexp you’re after is
/\n\s+\n/: a newline, any amount of space-like characters and another newline.However you’d need to save the result, too:
text = text.replace(...).http://jsfiddle.net/Mpvyk/2/