I have a regular expression, say /url.com\\/([A-Za-z]+)\\.html/
, and I would like to replace it with new string $1: f($1)
, that is, with a constant
.replace() takes a function for the replace, like this:
var newStr = string.replace(/url.com\/([A-Za-z]+)\.html/, function(all, match) {
return match + " something";
});
You can transform the result however you want, just return whatever you want the match to be in that callback. You can test it out here.