美文网首页
js \n replace 全局 替换

js \n replace 全局 替换

作者: 他大舅啊 | 来源:发表于2019-08-17 11:31 被阅读0次

使用带有g标识的正则, 可以搜索并替换所有匹配的字符

var txts = $('.wra').html();
txts=txts.replace(/\n/g,'<br>')
$('.wra').html(txts);

换行符因为操作系统不同会有差异, 因此使用下面的方法更加安全:

var txts = $('.wra').html();
txts=txts.replace(/[\n\r]/g,'<br>')
$('.wra').html(txts);

相关文章

网友评论

      本文标题:js \n replace 全局 替换

      本文链接:https://www.haomeiwen.com/subject/ebmhsctx.html