是否可以通过.replace替换字符串中的所有回车符? [英] Is it possible to replace all carriage returns in a string via .replace?

查看:565
本文介绍了是否可以通过.replace替换字符串中的所有回车符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 .replace 函数替换字符串中的所有回车符?我发现了很多复杂的功能,但是想知道它是否只需通过 .replace 的正则表达式进行简化?

Is it possible to replace all carriage returns in a string with the .replace function? I've found quite a few complex functions to do it, but was wondering if it could be simplified with just a regex through .replace?

谢谢!

推荐答案

\ n(新行)和\ r(carraige返回)创建一个新行。要同时替换两者的所有实例:

Both \n (new line) and \r (carraige return) create a new line. To replace all instances of both at the same time:

s.replace(/[\n\r]/g, '');

请注意,您可能希望用一个空格而不是空格替换它们。

Note that you might want to replace them with a single space rather than nothing.

这篇关于是否可以通过.replace替换字符串中的所有回车符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆