【发布时间】:2013-10-31 23:52:32
【问题描述】:
在处理 javascript 正则表达式时,我并没有真正理解 capturing parentheses 的概念。我不明白为什么下面的例子需要括号
var x = "{xxx} blah blah blah {yyy} and {111}";
x.replace( /{([^{}]*)}/g ,
function(match,content) {
console.log(match,content);
return "whatever";
});
//it will print
{xxx} xxx
{yyy} yyy
{111} 111
所以当我从模式x 中删除括号时,结果会给出不同的值
x.replace( /{[^{}]*}/g ,
function(match,content) {
console.log(match,content);
return "whatever";
});
//it will print
{xxx} 0
{yyy} 37
{111} 49
所以内容值现在变成了我不知道为什么的数值。有人可以解释幕后发生的事情吗?
【问题讨论】:
-
第二个代码将输出 0,21,31 这是匹配的索引(不是您提供的)
标签: javascript regex replace str-replace