Flash AS3: How to parse regex found in parentheses in replacement string?
I want to do 开发者_如何学编程something like this:
"3*4".replace(/([0-9]+)[*]([0-9]+)/g, String(Number("$1") * Number("$2")))
And no, i don't want to do that, but something more complex.
"3*4".replace(/(\d+)\*(\d+)/g, function():String {
return String(Number(arguments[1]) * Number(arguments[2]));
});
This can also be done in straight JavaScript like so:
function process(text) {
return text.replace(/(\d+)\*(\d+)/g, function(m0, m1, m2) {
return m1.valueOf() * m2.valueOf();
});
}
精彩评论