【发布时间】:2012-01-23 17:28:44
【问题描述】:
我有意见
<input>
当有人将多行文本粘贴到输入中时,我需要将换行符(\r、\n 和 \r\n)和制表符 \t 替换为逗号 ,。大多数情况下,当用户复制一列或一行时,输入将来自 Microsoft Excel 或 Apple Numbers。
我目前正在尝试这个 jQuery
$(function(){
// jQuery paste event. how trendy!
$('input').on('paste', function(e){
var e = $(this);
setTimeout(function(){
e.val( $.trim(e.val()).replace(/\s+/g, ',') );
}, 0);
});
});
问题
他们的电子表格中的某些单元格中可能包含空格,因此 /\s+/g 将过于宽泛。但是,/[\r\n\t]+/g 不起作用。事实上,我认为 jQuery 在删除换行符之前甚至无法访问输入的值。
在 jsFiddle 上一起唱歌
我提供了一个 jsfiddle 预期的输入和输出
<!-- sample input, copy below this line --
one
2
hello world
foo bar
m3000
-- end copy -->
<!-- output should look like
one, 2, hello world, foo bar, m3000
-->
【问题讨论】:
标签: javascript jquery regex clipboard jquery-events