Remove the Vowels from a String in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
2 min

banner

# Remove the Vowels from a String in JavaScript

Use the replace() method to remove the vowels from a string, e.g. str.replace(/[aeiou]/gi, '');.

The replace() method will return a new string where all vowels in the original string have been replaced by empty strings.

index.js
const str = 'bobbyhadz.com'; const noVowels = str.replace(/[aeiou]/gi, ''); console.log(noVowels); // ๐Ÿ‘‰๏ธ bbbyhdz.cm

remove vowels from string

The code for this article is available on GitHub

The String.replace() method returns a new string with one, some, or all matches of a regular expression replaced with the provided replacement.

The method takes the following parameters:

NameDescription
patternThe pattern to look for in the string. Can be a string or a regular expression.
replacementA string used to replace the substring match by the supplied pattern.

The first argument we passed to the String.replace() method is a regular expression.

The forward slashes / / mark the beginning and end of the regular expression.

index.js
const str = 'bobbyhadz.com'; const noVowels = str.replace(/[aeiou]/gi, ''); console.log(noVowels); // ๐Ÿ‘‰๏ธ bbbyhdz.cm
The part in the square brackets [] is called a character class and matches any of the characters in the brackets, in our case - all vowels.

For example, [abc] matches the characters a, b and c.

We used the g (global) flag because we want to match all occurrences of a vowel in the string and not just the first occurrence.

The i flag is used to make the search case-insensitive.

index.js
const str = 'bObbyHAdz.com'; // โœ… "i" flag is set (remove uppercase, lowercase vowels) const noVowels1 = str.replace(/[aeiou]/gi, ''); console.log(noVowels1); // ๐Ÿ‘‰๏ธ bbbyHdz.cm // ------------------------------------------------ // โœ… "i" flag NOT set (remove only lowercase vowels) const noVowels2 = str.replace(/[aeiou]/g, ''); console.log(noVowels2); // ๐Ÿ‘‰๏ธ bObbyHAdz.cm

The following two regular expressions are the same:

  • /[aeiou]/gi
  • /[aeiouAEIOU]/g

The second argument we passed to the replace() method is the replacement string for each match.

index.js
const str = 'bobbyhadz.com'; const noVowels = str.replace(/[aeiou]/gi, ''); console.log(noVowels); // ๐Ÿ‘‰๏ธ bbbyhdz.cm

We want to remove each vowel from the string, so we replace each match with an empty string.

# Defining a reusable function

If you have to do this often, define a reusable function.

index.js
function removeVowels(str) { return str.replace(/[aeiou]/gi, ''); } const result1 = removeVowels('bobbyhadz.com'); console.log(result1); // ๐Ÿ‘‰๏ธ bbbyhdz.cm const result2 = removeVowels('abcde'); console.log(result2); // ๐Ÿ‘‰๏ธ bcd

defining reusable function

The code for this article is available on GitHub

The removeVowels() function takes a string as a parameter and returns a new string where all vowels of the original string are removed.

The String.replace() method returns a new string with the matches of the pattern replaced. The method doesn't change the original string.

Strings are immutable in JavaScript.

If you ever need help reading a regular expression, check out this regular expression cheat sheet by MDN.

It contains a table with the name and the meaning of each special character with examples.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev