js 字符串 拆分成数组 目标字符串:string strNum="12,,34,,32,,44,";

拆分: strNum.split(",,")
结果:strNum=["12","34","32","44,"]
这个会有问题,不是我想要的结果,请求大神指教!
期望拆分后的数组:strNum=["12,","34,","32,","44,"]

如果使用正则,那麽目标字符串是:
string strNum2="|12,,|34,,|32,,|44,";
期望的结果是:strNum=["|12,","|34,","|32,","|44,"]
最新回答
深碍至白头

2025-03-30 04:52:27

var str = "12,,34,,32,,44,";
var reg = /\d+\,/g;
var strNum = str.match(reg);
console.log(strNum);

采纳即可

追问
那若果我的目标字符串是:"|346,,|23,,|78,"
那该如何拆分成 ["|346,","|23,","|78,"]
追答
var reg = /\|\d+\,/g;

最后,采纳即可
采纳即可
采纳即可
追问
ok
拆分后["|144,", "|145,", "|363,", "|364,", "|413,"]
散步的月光

2025-03-30 03:36:12

strNum.split(",+")

这样试试,匹配一个或以上的逗号