2

我想通过这个正则表达式来遍历字符串的内容,并简单地将一对数字升序。

例子

从:

<b>text</b> "1"  <b>text</b> "1"
<b>text</b> "1"  <b>text</b> "1"
<b>text</b> "1"  <b>text</b> "1"
...

至:

<b>text</b> "1"  <b>text</b> "1"
<b>text</b> "2"  <b>text</b> "2"
<b>text</b> "3"  <b>text</b> "3"
until the nth number...

上面的片段仅代表需要替换的部分,其中“文本”和标签可以是任何东西,因为我无法控制 html 输入......

查看当前脚本的外观:http: //jsfiddle.net/cVQEj/

4

1 回答 1

0

如果我理解正确,这应该会有所帮助:

演示

return temp.replace(/"(\d)"(.*?)"\1"/g, function (all, i, whitespace) {
    n += 1;
    return '"' + n + '"' + whitespace + '"' + n + '"';
});

临时字符串:

id="1" some text "1",  id="1" some text "1",  id="1" some text "1",

输出:

id="1" some text "1",  id="2" some text "2",  id="3" some text "3",
于 2013-07-09T11:23:33.253 回答