我想要做的是允许 2 个换行符进行格式化,但阻止显示 3 个或更多(在提交之前的预览中,所以不能用 php 将其删除)
我从
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n){2,}/g, '$1'+ breakTag +'$2');
我尝试添加{2,}
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n){2,}/g, '$1'+ breakTag +'$2');
提供于<textarea>
this is user input
i have inserted 1 line break
i have inserted 3 line breaks
i have inserted 4 line breaks
我想要的是
this is user input
i have inserted 1 line break
i have inserted 3 line breaks
i have inserted 4 line breaks
我得到的是
this is user inputi have inserted 1 line break
i have inserted 3 line breaks
i have inserted 4 line breaks
没有 {2,} 它显示为输入
如果我们能阻止他们在 textarea 开始时连续输入超过 2 个换行符,以及在预览时清理它
* 编辑 *
$('textarea').keyup(function()
{
var myString = $(this).val();
myString = nl2br(myString);
$('#preview').html(myString);
});
function nl2br (str, is_xhtml)
{
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}