简体   繁体   English

使用javascript .replace正则表达式将括号内的数字换行

[英]Wrap Numbers in Brackets using javascript .replace regex

I have this String that I am trying to wrap Brackets around the arrays of numbers following 'Position' and Color. 我有这个字符串,我想将方括号括在“位置”和“颜色”之后的数字数组周围。

str = 'Label_3_1:{Position: 115,234,Width: 126,Height: 20,Text:"Another Button",FontSize: 18,Color: 0,0,0,1}' str ='Label_3_1:{位置:115,234,宽度:126,高度:20,文本:“另一个按钮”,字体大小:18,颜色:0,0,0,1}”

I can use this regex to do it, but only if the numbers have a space after each comma 我可以使用此正则表达式来执行此操作,但前提是数字在每个逗号后都有空格

str = str.replace(/([\d\.]+(, [\d\.]+)+)/g, "[$1]");

I am trying to make it work without any spaces. 我正在尝试使其没有任何空格。

Output should look like this 输出应如下所示

str = 'Label_3_1:{Position: 115,234,Width: 126,Height: 20,Text:"Another Button",FontSize: 18,Color: [0, 0, 0, 1] }'

by add \\s* , it works 通过添加\\s* ,它可以工作

str.replace(/([\\d\\.]+(,\\s*[\\d\\.]+)+)/g, "[$1]");

And this is the result: 结果如下:

"Label_3_1:{Position: [115,234],Width: 126,Height: 20,Text:"Another Button",FontSize: 18,Color: [0,0,0,1]}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM