Regex for removing Commas within brackets

血红的双手。 提交于 2021-02-05 06:36:05

问题


I am stuck at writing the regex which needs me to remove comma inside brackets. The commas outside should stay as is

Here's what I need

Input :(37.400809377128354, -122.05618455969392) , (37.3931723332768, -121.89276292883454)

Output :(37.400809377128354 -122.05618455969392) , (37.3931723332768 -121.89276292883454)

Thanks in Advance!


回答1:


You can use this regex which uses positive look ahead to discard matching a comma that is outside the parenthesis,

,(?=[^()]*\))

Demo

var s = "(37.400809377128354, -122.05618455969392),(37.3931723332768, -121.89276292883454)";
console.log(s.replace(/,(?=[^()]*\))/g, ''));



回答2:


ES '18 Solution using Look-behind Assertions

const s = "(37.400809377128354, -122.05618455969392),(37.3931723332768, -121.89276292883454)";

console.log(
    s.replace(/(?<=\d),/g, '')
);

ECMAScript® 2018 Language Specification



来源:https://stackoverflow.com/questions/54359053/regex-for-removing-commas-within-brackets

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!