【问题标题】:Wrap Numbers in Brackets using javascript .replace regex使用 javascript .replace 正则表达式将数字括在括号中
【发布时间】:2013-01-06 15:50:37
【问题描述】:

我有这个字符串,我试图将括号括在后面的数字数组中 “位置”和颜色。

str = 'Label_3_1:{位置:115,234,宽度:126,高度: 20,Text:"Another Button",FontSize: 18,Color: 0,0,0,1}'

我可以使用这个正则表达式来做到这一点,但前提是每个逗号后面的数字都有一个空格

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

我正在尝试让它在没有任何空格的情况下工作。

输出应该是这样的

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

【问题讨论】:

  • 那个字符串是从哪里来的?如果可能,发送编码为 JSON 的字符串,然后在 JS 中解码。

标签: javascript regex string


【解决方案1】:

通过添加\s*,它可以工作

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

结果如下:

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

【讨论】:

    猜你喜欢
    • 2019-08-25
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    相关资源
    最近更新 更多