-2

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.

  • 2
    Could you please edit your question and fix the desired outputs you added as comments in the code, so that it's more clear what you want in each case? Also, I keep fixing your `"ban*n*"` because you have "bn*n*", but it always gets overwritten – blex Jul 08 '20 at 17:51
  • The forum markdown rules keep messing with my input. I'm trying to get to this result: `ban*n*` – bobbieganujsh Jul 08 '20 at 17:56
  • Do you realize it saying match "x", it is not the variable? – epascarello Jul 08 '20 at 17:56
  • @blex I've been trying to do that myself but but the forum keeps overriding my updates, too, and will not permit me to repost once I've edited it. `ban*n*` – bobbieganujsh Jul 08 '20 at 17:58

1 Answers1

2

Version 1: case sensitive

You could:

  1. Split the string on the character
  2. Join the string back together with *
  3. Replace the first * with the character

function charReplace (string, x) {
  return string.split(x).join('*').replace('*', x);
};

console.log(charReplace("banana", "a")); // output ban*n*
console.log(charReplace("banana", "n")); // output bana*a
console.log(charReplace("Apple",  "p")); // output Ap*le

Version 2: case-insensitive

You can use the RegExp constructor to use a variable in your regex:

function charReplace (string, x) {
  let isFirstMatch = true;
  return string.replace(new RegExp(`(${x})`, "gi"), (char) => {
    if (isFirstMatch) {
      isFirstMatch = false;
      return char;
    }
    return '*';
  });
};

console.log(charReplace("banana", "a")); // output ban*n*
console.log(charReplace("banana", "n")); // output bana*a
console.log(charReplace("Apple",  "p")); // output Ap*le
console.log(charReplace("ApPle",  "p")); // output Ap*le
blex
  • 24,941
  • 5
  • 39
  • 72
  • That works perfectly. I never would've thought of this simple solution. FYI, I'm a coding student, just learning my way around JS. Everyday is like starting over for me. Thanks for your help! – bobbieganujsh Jul 08 '20 at 18:03
  • @bobbieganujsh Cool! Careful though, that first version was case sensitive (would not replace any uppercase "A" if you give it a lowercase "a". I've added a second version handling that case – blex Jul 08 '20 at 18:10
  • Excellent and duly noted. Appreciate the additional example giving case a nod. – bobbieganujsh Jul 08 '20 at 18:31