我想转换"categories": ["Doctors", "Health & Medical"],
转到"categories": ["Doctors, Health & Medical"],
换句话说,我想找到" ," (在开放的qoute和逗号中间有空格)。
然后用,替换
但是," ,"在文档的其他地方重复,因此仅使用" ,"是不够的。最好的方法就是在" ,"中找到[...]
例如:“类别”:“医生”、“健康与医疗”、“城市”:“凤凰”、“计数”:9
先谢谢你
发布于 2015-12-22 04:41:55
正如注释中指出的,如果可以的话,您应该避免regex。
在JavaScript中用一行实现这样的目标是非常简单的。
例如,您可以只使用.join()方法:
obj.categories = [obj.categories.join(', ')];但是,如果您没有选择,可以使用以下方法:
/((?:\[[^\]]*?)|(?:(?<!^)\G[^\]]*?))(?:", ")(?=[^\]]*"\])/g然后将匹配替换为\1,。
基于测试用例,字符串:
"categories": ["Doctors", "Health", "Other"], "city": "Phoenix", "count": 9将产出:
"categories": ["Doctors, Health, Other"], "city": "Phoenix", "count": 9https://stackoverflow.com/questions/34406921
复制相似问题