Javascript:传递带有要替换的匹配项(regex,func(arg))的函数不';行不通

Javascript:传递带有要替换的匹配项(regex,func(arg))的函数不';行不通,javascript,regex,google-chrome,replace,arguments,Javascript,Regex,Google Chrome,Replace,Arguments,根据这个网站的说法,下面的替换方法应该有效,尽管我对此表示怀疑。 我的代码如下: text = text.replace( new Regex(...), match($1) //$.. any match argument passed to the userfunction 'match', // which itself invokes a userfunction ); 我使用的是Chrome14,没有传递任何参数给函数matc

根据这个网站的说法,下面的替换方法应该有效,尽管我对此表示怀疑。

我的代码如下:

text = text.replace( 
    new Regex(...),  
    match($1) //$.. any match argument passed to the userfunction 'match',
              //    which itself invokes a userfunction
);
我使用的是Chrome14,没有传递任何参数给函数match

更新

它在使用时起作用

text.replace( /.../g, myfunc($1) );
JavaScript解释器期望关闭,-明显的用户函数似乎会导致范围问题,即不会调用更多的用户函数。起初我希望避免闭包以防止必要的内存消耗,但现在已经有了保护措施

要将参数传递给您自己的函数,请执行以下操作(其中参数[0]将包含整个匹配项:

result= text.replace(reg , function (){
        return wrapper(arguments[0]);
});
此外,我在字符串转义和正则表达式中遇到问题,如下所示:

text = text.replace( 
    new Regex(...),  
    match($1) //$.. any match argument passed to the userfunction 'match',
              //    which itself invokes a userfunction
);
/\s……\s/g

不一样

newregex(“\s……\s”,“g”)

newregex('\s…\s',“g”)


所以要小心!

我认为您正在寻找新的RegExp(模式、修饰符)


$1必须在字符串内:

"string".replace(/st(ring)/, "gold $1") 
// output -> "gold ring"
具有以下功能:

"string".replace(/st(ring)/, function (match, capture) { 
    return "gold " + capture + "|" + match;
}); 
// output -> "gold ring|string"

是的,在字符串中,$1由replace函数解析。在代码示例中,$1只是任何变量,取自链接中的示例。--最好传递参数[0]尽管如此,因为最后一个参数将包含整个文本。我花了一些时间来思考,但这正是我所需要的。您能澄清一下使用
text.replace(/…/g,myfunc($1))的意思吗
?我认为这实际上不起作用,因为
$1
必须是一个字符串。请参阅我对Joe答案的评论,这里有一个例子:我仍然不确定我是否理解。在其中使用函数对我来说是有意义的,但期望
$1
有一个值是没有意义的。修饰符对于他的情况来说是足够的。