【发布时间】:2012-12-20 12:28:51
【问题描述】:
我有这个字符串,我试图通过添加一个 _# 来增加每个单词/属性,其中 # 是 { 左侧的递增数字。
str = 'View{Image{BackgroundImage: Image.png;Position: 0, 0;Width: 320;Height: 480;}Button{BackgroundImage: ButtonTop.png;Position: 61, 83;Width: 217;Height: 58;}Button_2{BackgroundImage: ButtonBottom.png;Position: 21, 303;Width: 217;Height: 58;}}'
过去我使用过这个正则表达式,但它不起作用,我不知道为什么。
var i = {};
str = str.replace(/(\S+):{/g, function (m, p1) {
i[p1] = (i[p1] || 0) + 1;
return p1 + "_" + i[p1].toString() + ":{";
});
期望的输出是:
str = 'View_1{Image_1{BackgroundImage: Image.png;Position: 0, 0;Width: 320;Height: 480;}Button_1{BackgroundImage: ButtonTop.png;Position: 61, 83;Width: 217;Height: 58;}Button_2{BackgroundImage: ButtonBottom.png;Position: 21, 303;Width: 217;Height: 58;}}'
【问题讨论】:
-
你明白这是什么意思吗:
\S+?
标签: javascript regex string auto-increment