1

I currently have code to strip any formatting on paste so the user is just pasting plain text, however it is not stripping line breaks. I have tried adding the line text = text.replace(/(\r\n\t|\n|\r\t)/gm, ""); to remove the line breaks, but this is not working. I need this to function within IE 11, how can I accomplish this?

 $('[contenteditable]').on('paste', function (e) {
    e.preventDefault();
    var text = '';
    if (e.clipboardData || e.originalEvent.clipboardData) {
        text = (e.originalEvent || e).clipboardData.getData('text/plain');
    } else if (window.clipboardData) {
        text = window.clipboardData.getData('Text');
    }
    if (document.queryCommandSupported('insertText')) {
        document.execCommand('insertText', false, text);
    } else {
        document.execCommand('paste', false, text);
    }
});
Stormy
  • 49
  • 8
  • Possible duplicate of [How to remove all line breaks from a string?](https://stackoverflow.com/questions/10805125/how-to-remove-all-line-breaks-from-a-string) – Reinstate Monica Cellio Oct 03 '18 at 13:48
  • @archer I have tried implementing the suggested fix from that post, however it did not solve my problem – Stormy Oct 03 '18 at 13:59
  • 2
    You must have done something wrong, as it works fine. See here... http://jsfiddle.net/ArchersFiddle/tgc2ym34/1/ – Reinstate Monica Cellio Oct 03 '18 at 14:09
  • @Archer I have it formatted just like you exemplified, however it is still not working within IE 11. Is there something wrong with the code where it wouldn't function within IE? – Stormy Oct 03 '18 at 14:18
  • I tested this fiddle: http://jsfiddle.net/ArchersFiddle/tgc2ym34/1/ in IE11 and it works fine. There's something else you're doing wrong @Stormy – Consta Gorgan Oct 03 '18 at 14:26
  • Oh, I figured out the problem. You have some typos in your RegEx. Replace this: `text.replace(/(\r\n\t|\n|\r\t)/gm, "")` with this `text = text.replace(/\r?\n|\r/g, "")` I will post an answer. – Consta Gorgan Oct 03 '18 at 14:30
  • @ConstaGorgan That's exactly what is in the answer I linked and the consequent JSFiddle. Don't copy from existing answers to make new ones. – Reinstate Monica Cellio Oct 03 '18 at 14:39
  • I am just trying to help here, maybe he doesn't have an up-to-date version of jQuery, or maybe it's not loaded at all. Let's try to be useful rather than just blame him :D – Consta Gorgan Oct 03 '18 at 15:02
  • @ConstaGorgan No-one is "blaming" anyone - I think you've misunderstood the situation. OP has posted code and I've posted a working and verified solution. He has since said that this still does not work but you decided to post it as an answer anyway. It would be better if you'd just stick to posting comments until you know for sure what's wrong with the OP's code. – Reinstate Monica Cellio Oct 03 '18 at 15:47
  • @archer I've tried to simplify this by changing the code to this function removeFormatOnPaste() { $(document).on('paste', function () { var text = $(this); setTimeout(function () { text = text.replace(/(\r\n\t|\n|\r\t)/gm, "") }) }); However this is not working either, do you have any suggestions on simplifying the code? – Stormy Oct 03 '18 at 16:48
  • @Archer as I said, maybe he is just using a **wrong jQuery version**. I was just trying to help for god sake. Nevermind, I will delete the answer. – Consta Gorgan Oct 03 '18 at 17:03
  • @ConstaGorgan You've never actually asked the OP *anything* about jQuery, which isn't relevant here anyway. The event handler is already working. I've told the OP what to do and they chose to completely change it and then say it didn't work. I can't help them any more. – Reinstate Monica Cellio Oct 03 '18 at 18:33

0 Answers0