js 去掉以逗号分割的字符串中头尾和中间多余的逗号

let permission = ",,,106,105,108,,,109,110,107,,101,,,"

let permission = "106,105,108,109,110,107,101"

我们要把上面的字符串处理成想要的结果,最好的方法是使用正则,我的正则一直写不太好,就想换个思路解决。

首先想到个最笨的办法,先掐头去尾再解决中间的,但这也不是理解的办法,要写一大行代码,还容易 出错

就想到先把它转换成数组,再对数组去空,然后再转回字符串,完美解决

let permission = ",,,106,105,108,,,109,110,107,,101,,,"
console.log("permission",permission)
arry = permission.split(",");
console.log("arry",arry)
let arry2 =[]
arry.map((item)=>{
if (item !== "" && item != undefined) {
arry2.push(item);
}
})
arry2 = arry2.join(',')
console.log("arry2",arry2)

js 去掉以逗号分割的字符串中头尾和中间多余的逗号

上一篇:mysql调优最大连接数


下一篇:【Python】协程