Questions tagged «backreference»

5
JavaScript-字符串正则表达式反向引用
您可以在JavaScript中像这样反向引用: var str = "123 $test 123"; str = str.replace(/(\$)([a-z]+)/gi, "$2"); 这(很愚蠢)将“ $ test”替换为“ test”。但是想象一下,我想将所得的$ 2字符串传递给一个函数,该函数返回另一个值。我尝试这样做,但没有得到字符串“ test”,而是得到了“ $ 2”。有没有办法做到这一点? // Instead of getting "$2" passed into somefunc, I want "test" // (i.e. the result of the regex) str = str.replace(/(\$)([a-z]+)/gi, somefunc("$2"));
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.