4

您能否解释一下为什么我无法从匹配的正则表达式结果中获取反向引用的值并在有效替换之前对其进行一些修改?

预期的结果是将例如字符串替换".coord('X','Y')""X * Y". 但是如果X> 到某个值,则将该值除以 2,然后在替换中使用这个新值。

这是我目前正在测试的代码:

/*>>1<<*/& /*>>2<<*/& /*>>3<<*/,这就是我卡住的地方!

我希望能够根据反向引用值在替换之前对反向引用进行修改。

/*>>2<<*/&之间的区别/*>>3<<*/只是自调用匿名函数参数

该方法/*>>2<<*/i可以理解的预期工作解决方案。但奇怪的是,替换工作不正常,用别名$1 * $2而不是值替换......?

你可以测试jsfiddle

//string to test
".coord('125','255')"

//array of regex pattern and replacement //just one for the example
//for this example, pattern matching alphanumerics is not necessary (only decimal in coord) but keep it as it
var regexes = [ //FORMAT is array of [PATTERN,REPLACEMENT]
    /*.coord("X","Y")*/ [/\.coord\(['"]([\w]+)['"],['"]?([\w:\.\\]+)['"]?\)/g, '$1 * $2']
                  ];
function testReg(inputText, $output) {
    //using regex
    for (var i = 0; i < regexes.length; i++) {
        /*==>**1**/ //this one works as usual but dont let me get backreferences values
         $output.val(inputText.replace(regexes[i][0], regexes[i][2]));

         /*==>**2**/ //this one should works as i understand it
         $output.val(inputText.replace(regexes[i][0], function(match, $1, $2, $3, $4) { 
            $1 = checkReplace(match, $1, $2, $3, $4);
            //here want using $1 modified value in replacement
            return regexes[i][3]; 
        }));

         /*==>**3**/ //this one is just a test by self call anonymous function
         $output.val(inputText.replace(regexes[i][0], function(match, $1, $2, $3, $4) {
            $1 = checkReplace(match, $1, $2, $3, $4);
            //here want using $1 modified value in replacement
            return regexes[i][4];
        }()));

        inputText = $output.val();
    }
}

function checkReplace(match, $1, $2, $3, $4) {
    console.log(match + ':::' + $1 + ':::' + $2 + ':::' + $3 + ':::' + $4);
    //HERE i should be able if lets say $1 > 200 divide it by 2
    //then returning $1 value
    if($1 > 200) $1 = parseInt($1 / 2);
    return $1; 
}​

当然我错过了一些东西,但无法得到它!

谢谢你的帮助,问候。

编辑工作方法: 终于得到它,正如 Eric 提到的:

关键是该函数返回要替换的文字文本,而不是为反向引用而解析的字符串。​​</p>

JSFIDDLE

所以完整的工作代码:(请注意,每个匹配的模式都会改变模式替换,速度代码的优化在这里不是问题,我会保持这样)

 $('#btn').click(function() {
    testReg($('#input').val(), $('#output'));
});

//array of regex pattern and replacement //just one for the example
var regexes = [ //FORMAT is array of [PATTERN,REPLACEMENT] /*.coord("X","Y")*/ 
    [/\.coord\(['"]([\w]+)['"],['"]?([\w:\.\\]+)['"]?\)/g, '$1 * $2']
                     ];

function testReg(inputText, $output) {
    //using regex
    for (var i = 0; i < regexes.length; i++) {
        $output.val(inputText.replace(regexes[i][0], function(match, $1, $2, $3, $4) {
            var checkedValues = checkReplace(match, $1, $2, $3, $4);
            $1 = checkedValues[0];
            $2 = checkedValues[1];
            regexes[i][1] = regexes[i][1].replace('$1', $1).replace('$2', $2);
            return  regexes[i][1];
        }));
        inputText = $output.val();
    }
}

function checkReplace(match, $1, $2, $3, $4) {
    console.log(match + ':::' + $1 + ':::' + $2 + ':::' + $3 + ':::' + $4);
    if ($1 > 200) $1 = parseInt($1 / 2);
    if ($2 > 200) $2 = parseInt($2 / 2);
    return [$1,$2];
}​
4

2 回答 2

3
.replace(regexes[i][0], function(match, $1, $2) {
    if ($1 > 200) $1 = parseInt($1 / 2);
    if ($2 > 200) $2 = parseInt($2 / 2);
    return $1 + "*" + $2;
})); 
于 2012-10-20T10:24:19.707 回答
1

如果我理解正确,你没有得到你想要的结果,因为

'str'.replace( /(str)/, function(match,a){return '$1';} ) // "$1"

因此,您的初始数组不是根据您的使用方式构建的。
尝试类似的东西

var regexes = [
    [ /\.coord\(['"]([\w]+)['"],['"]?([\w:\.\\]+)['"]?\)/g, function(match, $1, $2){return $1 + ' * ' + $2;} ]
];

并调用

return regexes[i][1](match, $1, $2, $3, $4);
于 2012-10-20T10:24:09.827 回答