我有以下str,我想通过删除其中的字符"/“并将其转换为数组来清理它。str看起来像这样:
var str = `“01 Lima / / ”
“01 Lima / 50 Lima / ”
“01 Lima / 51 Barranca / ”
“01 Lima / 50 Lima / 202 La Molina”
“01 Lima / 50 Lima / 203 San Isidro”
“02 Arequipa / / ”
“02 Arequipa / 63 Arequipa / ”
“02 Arequipa / 64 Caylloma / ”
“02 Arequipa / 63 Arequipa / 267 Cercado”`我的理想输出将如下所示:
["01 Lima", "01 Lima 50 Lima"] and so on我编写了以下代码,对输入进行了一些清理,但并不完全符合我的需要:
const ubigeoParts=str.replace(/""/g,'').split('/');关于如何实现我想要的东西,有什么建议吗?
发布于 2019-07-30 05:42:37
如果有帮助的话
分成两行
let replaceStr = str.replace(/\“|\”|\/|/g,'').split("\n");
replaceStr = replaceStr.map((elem) => elem.replace(/\s+/g,' ').trim());
console.log(replaceStr)在一行中
let replaceStr = str.replace(/\“|\”|\/|/g,'').split("\n").map((elem) => elem.replace(/\s+/g,' ').trim());
console.log(replaceStr)发布于 2019-07-30 05:28:56
这可能不是最优化的方法,但它应该可以完成工作
str.match(/“(.+)”/g).map(s => s.replace(/“|”|\s*\/\s*/g, ' ').trim())发布于 2019-07-30 05:30:32
您可以使用拆分\n获取每一行,然后用space替换"/"
var str = `“01 Lima / / ”
“01 Lima / 50 Lima / ”
“01 Lima / 51 Barranca / ”
“01 Lima / 50 Lima / 202 La Molina”
“01 Lima / 50 Lima / 203 San Isidro”
“02 Arequipa / / ”
“02 Arequipa / 63 Arequipa / ”
“02 Arequipa / 64 Caylloma / ”
“02 Arequipa / 63 Arequipa / 267 Cercado”`
const res = str.split("\n").map(el => el.replace(/\s*\/\s*/g, " "))
console.log(res)
https://stackoverflow.com/questions/57261550
复制相似问题