Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Content editable remove unnecessary tags with content and all html tags on paste
$('[contenteditable]').on('paste',function(e) {
var text = (e.originalEvent || e).clipboardData.getData('text/html');
var $result = $('<div></div>').append($(text));
// remove unnecesary tags (if paste from word)
var html = $result.html()
.replace(/<\/?[^`]+?\/?>/gmi, '\n') //replace all tags
.replace(/\n[\s]*\n/gmi, '\n'); //replace many empty lines to one
html = $.trim(html);
var lines = html.split('\n');
for(var l in lines){
lines[l] = '<div>' + $.trim(lines[l]) + '</div>';
html = lines.join('\n');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment