I’m using an extremely ugly function in jQuery to listen on a paste event and remove all extraneous HTML formatting from the paste. Unfortunately, what I have now is overly strict, on top of just being butt-ugly.
I want to improve this regex to allow for the same HTML I’m already allowing inside the WYSIWYG editor. This means I would like to have <b>, <i>, <br>, and <a> tags allowed.
I do not know enough regex to do this myself, and would be very appreciative to see this improved.
$('iframe').ready(function() {
$(this).contents().find('.wysiwyg').find('iframe').contents()
.find('.wysiwyg').bind('paste', function() {
var el = $(this);
setTimeout(function() {
var strClean = el.text().replace(/<\/?[^>]+>/gi, '');
el.text(strClean);
}, 0);
});
});
You can see this ugly code at work here: http://jsfiddle.net/v4LhV/3/
As you have a fully functional DOM parser, namely the browser, at hand, why not just parse the whole thing using
.html()in to an element (off screen), then run through removing stuff you do not want using.unwrap().