I would like do do the following in Javascript (pseudo code):
myString.replace(/mypattern/g, f(currentMatch));
that is, replace string isn't fixed, but function of current match.
I would like do do the following in Javascript (pseudo code):
myString.replace(/mypattern/g, f(currentMatch));
that is, replace string isn't fixed, but function of current match.
Just omit the argument, i.e. use this:
myString.replace(/mypattern/g, f);
Here's an example: http://ejohn.org/blog/search-and-dont-replace/
MDC claims that you can do just that:
function styleHyphenFormat(propertyName)
{
function upperToHyphenLower(match)
{
return '-' + match.toLowerCase();
}
return propertyName.replace(/[A-Z]/, upperToHyphenLower);
}
Or more generically:
myString.replace(/mypattern/g, function(match){
return "Some function of match";
});