Skip to content

Instantly share code, notes, and snippets.

@nhducit
Forked from AllThingsSmitty/replace.js
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhducit/1ae8579c6aed28fa7453 to your computer and use it in GitHub Desktop.
Save nhducit/1ae8579c6aed28fa7453 to your computer and use it in GitHub Desktop.
var myString = 'EXAMPLEstring';
var myNewString = myString.replace(/[A-Z]/g, '0');
console.log(myNewString);
function replaceFunc (a, b, c) {
console.log(a, b, c);
return a.toLowerCase();
}
var myOtherString = myString.replace(/[A-Z]/g, replaceFunc);
console.log(myOtherString);
// This function expects three arguments. There could be more depending on how many "capture" groups are included in the regular expression.
// The first argument (a) is always the full text of the match.
// Since there are no capture groups, the second argument (b) is the zero-based index of the match within the string.
// The final argument (c) is the full text of the string being searched.
// As shown in this example, because the match is global (using the "g" identifier in the RegEx), the three arguments are logged for each match.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment