Subscribe

Removing all vowels with JavaScript

✍️

Removing all vowels from a string with JavaScript

3 Dec, 2022 · 2 min read

Today we’ll look at a nifty solution to remove all vowels in a string using JavaScript.

The idea is that we get a string and have to return the string without the letters aeiou.

JavaScript remove all vowels

Let’s dive right into the solution.

const input = 'daily dev tips';

const removeVowels = input.replace(/[aeiou]/gi, '');

console.log(removeVowels);

// 'dly dv tps'

It works! And yep that’s all the code we need, but let’s take a closer look at how it works.

The first part is to use the JavaScript replace function to replace a specific match. The first part is the match, for which we’ll use a regular expression. And the second part is the replacement.

We could, say, replace the letter i with an empty string like this.

const removeVowels = input.replace('i', '');

console.log(removeVowels);

// 'daly dev tips'

However, you’ll notice it only replaced the first occurrence. And, of course, we can only target one letter at a time. So this is where our regular expression comes in.

We start the regular expression by wrapping it in the / / backslashes. Then we open a pattern matcher with the [] brackets. And in between, we specify which letters should be matched. The last part is to use gi at the end, which stands for global ignore.

  • global meaning to apply it to each occurrence, not just the first one
  • ignore means to perform a case-insensitive search

And that’s it. I hope you learned something about removing vowels with JavaScript.

Thank you for reading, and let’s connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next 📖

10 games to learn JavaScript

2 Dec, 2022 · 3 min read

10 games to learn JavaScript

Detecting if the user is online with JavaScript

1 Dec, 2022 · 4 min read

Detecting if the user is online with JavaScript

Join 2099 devs and subscribe to my newsletter