2

I am trying to use the following code to replace all the vowels from a string but it only works using regex. How can I use for of iteration?

 // THIS DOES NOT WORK
    newStr = '';
    for (const c of "aeiou") {
      console.log(c);
      newStr = 'Hello World'.replace(c, '*');
    }
    console.log(newStr); // Prints Hello World to the console
    
    // ONLY THIS WORKS
    newStr = 'Hello World'.replace(/[aeiou]/g, '*');
    console.log(newStr);
Maniraj Murugan
  • 8,868
  • 20
  • 67
  • 116
jaysonn
  • 105
  • 9
  • 3
    `newstr` will only ever be the result of the last iteration, replacing the first "u" character in "Hello World" which doesn't exist – Phil Jun 12 '20 at 06:37
  • This is not the solution but, how can the `const c` change it's value? – Tim567 Jun 12 '20 at 06:39
  • @Tim567 the `const` is block-scoped to the `for` loop – Phil Jun 12 '20 at 06:39
  • Why do you want to use the clunky, error-prone `for..of` when the regex works perfectly? – Phil Jun 12 '20 at 06:43
  • 1
    Related - [How to replace all occurrences of a string?](https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string) – Phil Jun 12 '20 at 06:45

4 Answers4

2

use split, join as a way if you don't want to use regex

newStr = 'Hello World';
    for (const c of "aeiou") {
      console.log(c);
      //newStr = 'Hello World'.replace(c, '*');
      newStr = newStr.split(c).join('*');
    }
    console.log(newStr); // Prints Hello World to the console
    
    // ONLY THIS WORKS
    newStr = 'Hello World'.replace(/[aeiou]/g, '*');
    console.log(newStr);
mr. pc_coder
  • 16,412
  • 3
  • 32
  • 54
1

The issue with your code is that in for loop everytime youare trying to replace on "Hello World" string and assign the result to newStr. However in the last iteration you check for u, since u is not presetn in Hello World, the entire string gets assigned as such to newStr.

You should instead initialise newStr to "Hello World" and then perform replace on it

var newStr = 'Hello World';
for (const c of "aeiou") {
  console.log(c);
  newStr = newStr.replace(c, '*');
}
console.log(newStr); 

However note that this will only replace one instance of the matching character and not all of them, You will still need to use regex

newStr = newStr.replace(new RegExp(c, 'g'), '*');

var newStr = 'Hello World';
for (const c of "aeiou") {
  newStr = newStr.replace(new RegExp(c, 'g'), '*');
}
console.log(newStr); 

or split and join the string

newStr = newStr.split(c).join('*');

var newStr = 'Hello World';
for (const c of "aeiou") {
  newStr = newStr.split(c).join('*');
}
console.log(newStr); 
There is a however proposal for using String.prototype.replaceAll on string
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
  • 2
    Still need to use a regex in the loop to replace _all_ the matching characters, ie `new RegExp(c, 'g')` – Phil Jun 12 '20 at 06:38
0

Try this instead:

let newStr = 'Hello World';
for (const c of "aeiou") {
    while (newStr.indexOf(c) > -1)
        newStr = newStr.replace(c, "*");
}
console.log(newStr); // Prints Hello World to the console

string.prototype.replace only replaces the first occurrence of c in newStr. string.prototype.replace

leisheng
  • 340
  • 1
  • 8
0

You can do it this way:

var newStr = 'Hello World';

for (let a of newStr) {
    for (let b of "aeiou") {
        if (a === b) {
            newStr = newStr.replace(a, '*');
        }
    }
}

console.log(newStr);