DEV Community

Cover image for Removing all vowels with JavaScript
Chris Bongers
Chris Bongers

Posted on β€’ Originally published at daily-dev-tips.com

18

Removing all vowels with JavaScript

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'
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (5)

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ β€’ β€’ Edited

This will not remove all vowels - accented vowels will remain - for example in the word cafΓ©. To make this fully work, you should remove the accents first:

"cafΓ©"
  .replace(/[aeiou]/gi, '')  // cfΓ©

"cafΓ©"
  .normalize('NFD')
  .replace(/[\u0300-\u036f]/g, '')
  .replace(/[aeiou]/gi, '')  // cf
Enter fullscreen mode Exit fullscreen mode
Collapse
 
calinzbaenen profile image
Calin Baenen β€’

To be fair 'Γ₯', 'Γ€', and 'ΓΆ' *technically* aren't accents. πŸ™ƒ    /j

Collapse
 
dailydevtips1 profile image
Chris Bongers β€’

Ah good point, indeed those are not taking care off in this example :)

Collapse
 
calinzbaenen profile image
Calin Baenen β€’ β€’ Edited

What does the "NFD" in .normalize(...) do?
I "know what it does", but when I read it from MDN I don't understand what it means.

Collapse
 
calinzbaenen profile image
Calin Baenen β€’

Don't forget 'Γ…', 'Γ„', and 'Γ–'!

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay