最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

replace all vowels in a string javascript - Stack Overflow

programmeradmin9浏览0评论

I am trying to write a function that will remove all vowels in a given string in JS. I understand that I can just write string.replace(/[aeiou]/gi,"") but I am trying to plete it a different way...this is what I have so far... thank you!

I first made a different function called IsaVowel that will return the character if it is a vowel...

function withoutVowels(string) {

var withoutVowels = "";
for (var i = 0; i < string.length; i++) {
    if (isaVowel(string[i])) {
 ***not sure what to put here to remove vowels***
       }
  }
    return withoutVowels;
}

I am trying to write a function that will remove all vowels in a given string in JS. I understand that I can just write string.replace(/[aeiou]/gi,"") but I am trying to plete it a different way...this is what I have so far... thank you!

I first made a different function called IsaVowel that will return the character if it is a vowel...

function withoutVowels(string) {

var withoutVowels = "";
for (var i = 0; i < string.length; i++) {
    if (isaVowel(string[i])) {
 ***not sure what to put here to remove vowels***
       }
  }
    return withoutVowels;
}
Share Improve this question asked May 19, 2017 at 22:11 llllllllll 271 silver badge4 bronze badges 2
  • You don't want to do anything there, you want to do something in the else clause. – Barmar Commented May 19, 2017 at 22:20
  • Mark answer if problem is resolved. – EyuelDK Commented May 20, 2017 at 5:55
Add a ment  | 

3 Answers 3

Reset to default 6

Use accumulator pattern.

function withoutVowels(string) {

  var withoutVowels = "";
  for (var i = 0; i < string.length; i++) {
      if (!isVowel(string[i])) {
        withoutVowels += string[i];
      }
    }
    return withoutVowels;
}

function isVowel(char) {
  return 'aeiou'.includes(char);
}

console.log(withoutVowels('Hello World!'));

I tried doing this problem by first splitting the string into an array, while also creating an array of vowels. Then go through each element in the string array and check whether it's in my vowel array. If it is not in my vowel array, push it to the withoutVowels array. At the end of the for loop, join all elements in the withoutvowels array and return.

function withoutVowels(string) {
            var strWithoutVowels =  [];
            string = string.split('');
            var vowels = ['a', 'e', 'i', 'o', 'u'];
            for (var i = 0; i < string.length; i++) {
                if (vowels.indexOf(string[i]) < 0) {
                    strWithoutVowels.push(string[i])
                }
            }
            strWithoutVowels = strWithoutVowels.join('');
            return strWithoutVowels;
        }
        console.log(withoutVowels('Hello World!'))

I think the easiest way is to use a regex; it's cleaner and faster pared to all your loops. Below is the code.

string.replace(/[aeiou]/gi, '');

the gi in the code means no matter the case whether uppercase or lowercase so long as its a vowel, it will be removed

发布评论

评论列表(0)

  1. 暂无评论