views:

38

answers:

2

What I want to do is str.replace(pattern, callback),

not simply str.replace(pattern, replace_pattern),

is it possible to do it in javascript?

+3  A: 

Why, yes, you can do exactly that: str.replace(pattern, function () { ... )).

Here's some documentation: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String/replace

deceze
Wow,I didn't know that ,thanks!
wamp
+1  A: 

Yes

var s2 = s1.replace(/regex/, function(whole, part1, part2, ...) { ... })

The function is passed the whole matched string as the first argument. If there are any capturing groups, those are passed as subsequent arguments.

Pointy