gpt4 book ai didi

javascript - js中如何用逗号分隔

转载 作者:行者123 更新时间:2023-12-03 04:36:12 27 4
gpt4 key购买 nike

我的输入类似于下面的字符串:

IN|1st Cross,1st Block,IN|282,Sector 19,IN|10th Floor,Winchester House,DN|id,fgh,FG|ag

我想要的应该是这样的:

IN|1st Cross,1st Block IN|282,Sector 19 IN|10th Floor,Winchester House DN|id,fgh FG|ag

请问如何实现上述结果?

最佳答案

给你:

function separateComaForCountry(str) {
// find a coma, that has any amount of letters after it that are not , nor | and then stop when you see either the end of the string or another |
var reg = /,([^|,]+(?:$|\|))/g;
return str.replace(reg, function(match, country){
return ' ' + country;
});
}

然后只需根据您的用例调用该函数即可。

separateComaForCountry("IN|1st Cross,1st Block,IN|282,Sector 19,IN|10th Floor,Winchester House,DN|id,fgh,FG|ag");
// outputs IN|1st Cross,1st Block IN|282,Sector 19 IN|10th Floor,Winchester House DN|id,fgh FG|ag

关于javascript - js中如何用逗号分隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43290290/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com