Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using String.replace() with regular expressions
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);
// Credit: Louis Lazarus
// 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
You can’t perform that action at this time.