Tyilo
Tyilo

Reputation: 30102

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.

Upvotes: 0

Views: 467

Answers (2)

ridgerunner
ridgerunner

Reputation: 34395

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();
        });
}

Upvotes: 0

Jakub Hampl
Jakub Hampl

Reputation: 40543

"3*4".replace(/(\d+)\*(\d+)/g, function():String {
  return String(Number(arguments[1]) * Number(arguments[2]));
});

Upvotes: 5

Related Questions