我的输入类似于下面的字符串:
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/