I'm trying to write a function that replaces with an asterisk repetitions of a single character string found within a string (of any length), for example, if the function arguments are "banana","a"
it should return:
ban*n*
-- and case is irrelevant here.
At present I'm attempting to achieve this with .replace and a regex.
function charReplace (string, x) {
string.replace(/x{2,}/gi, "*");
return string;
};
console.log(charReplace("banana","a")); // output banana
console.log(charReplace("banana","n")); // output banana
console.log(charReplace("Apple","p")); // output Apple
I also tried adding the repeat operator (+), but that threw an error.
function charReplace (string, x) {
string.replace(/x+{2,}/gi, "*");
at charReplace
return string;
};
console.log(charReplace("banana","a"));
console.log(charReplace("banana","n"));
console.log(charReplace("Apple","p"));
Error: // Uncaught SyntaxError: Invalid regular expression: /x+{2,}/: Nothing to repeat at charReplace
Thanks for your assistance.