javascript - 只用一个替换双换行符?
问题描述
我怎样才能删除每一个双换行符,只有一个?
var string = "this is a ↵↵↵↵ test there will ↵↵ be more ↵↵↵↵ newline characters"
像这样的东西
var string = "this is a ↵↵ test there will ↵ be more ↵↵ newline characters"
我已经尝试过了,但这会替换所有新行,我想保留单行
string.replace(/[\n\n]/g, '')
解决方案
[\n\n]
字符类作为Logical OR
. [\n\n]
这意味着匹配\n
或\n
。你需要的\n
是\n
. 所以只需删除[]
字符类。
let str = `this is a
test there will
be more
newline characters`
console.log(str.replace(/\n\n/g, '\n'))
console.log(str.replace(/\n+/g, '\n')) // <--- simply you can do this
推荐阅读
- javascript - 返回与 Turf JS 库相交的 Leaflet GeoJSON 多边形
- ruby - 怎么样:Date.parse("123 456 789") == 2019 年 5 月 3 日星期五?
- java - java流列表
- python-3.x - BeautifulSoup - 试图解析网站但不确定如何解析 json 脚本?
- excel - 根据唯一 ID 索引来自许多工作簿的信息
- azure - Azure 自定义 SOAP 连接器 SOAP 到 REST 与逻辑应用错误日期
- jquery - Jquery循环遍历div中的元素div
- ios - Firebase - 应用程序长时间无响应(因为数据同步?)
- oracle - Oracle如何将带有任何时区的时间戳转换为带有数据库服务器时区的日期
- python - Flask Application - 'NoneType' 类型的参数不可迭代