Last updated: Mar 4, 2024
Reading timeยท2 min
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.
const str = 'bobbyhadz.com'; const noVowels = str.replace(/[aeiou]/gi, ''); console.log(noVowels); // ๐๏ธ bbbyhdz.cm
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:
Name | Description |
---|---|
pattern | The pattern to look for in the string. Can be a string or a regular expression. |
replacement | A 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.
const str = 'bobbyhadz.com'; const noVowels = str.replace(/[aeiou]/gi, ''); console.log(noVowels); // ๐๏ธ bbbyhdz.cm
[]
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.
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.
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.
If you have to do this often, define a reusable function.
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
The removeVowels()
function takes a string as a parameter and returns a new
string where all vowels of the original string are removed.
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.
You can learn more about the related topics by checking out the following tutorials: