【发布时间】:2020-09-02 02:54:08
【问题描述】:
我想格式化一个没有多个空格的字符串。字符串可以有制表符、回车符或换行符。
示例 1:
hello
world
预期结果:hello
world
示例 2:hello world
预期结果:'hello world'
const formatString = (s) => {
const trimmed = s.trim();
const formated = trimmed.match(/\s/g)
return s.trim().replace(/\s+/g, ' ')
}
const str = `hello
world`
const result = formatString(str)
console.log(result)
【问题讨论】:
标签: javascript regex