[英]Javascript/Jquery - Removing break line when copying and pasting text to textarea
When I copy and paste text into a textarea, is there a way to ignore if there's line breaks and whitespaces?当我将文本复制并粘贴到文本区域时,如果有换行符和空格,有没有办法忽略?
For example, When I highlight text below and copy paste into the textarea:例如,当我突出显示下面的文本并将粘贴复制到文本区域时:
abcdef,
ghijkl,
mnopqrs
I want them to be in the text area:我希望他们在文本区域:
abcdef,ghijkl,mnopqrs
ignore any whitespaces and line breaks.忽略任何空格和换行符。
You can use a paste
event or input
event (which covers paste and key events) and a regex to remove line break characters.您可以使用paste
事件或input
事件(包括粘贴和键事件)和正则表达式来删除换行符。
For explanation of the regex used see https://stackoverflow.com/a/10805198/1175966有关使用的正则表达式的说明,请参阅https://stackoverflow.com/a/10805198/1175966
document.querySelector('textarea').addEventListener('input', function(){ this.value = this.value.replace(/(\r\n|\n|\r)/gm, ""); })
<textarea></textarea> <pre> abcdef, ghijkl, mnopqrs </pre>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.