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.
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";
});
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With