Navigate back to the homepage

How do you separate vowels and consonants in a string in Javascript?

Infinitbility
JavaScript
October 18th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To separate vowels and consonants in a string in javascript, use the match() method with /[aeiou]/gi regex it is the simplest way to separate vowels and consonants from a string.

Today, I’m going to show you How do I separate vowels and consonants in a string in javascript, as mentioned above, I’m going to create a sample string variable with data and use the match() method with /[aeiou]/gi regex to separate vowels and consonants.

Let’s start today’s tutorial on how do you separate vowels and consonants in a string in javascript.

Javascript separates vowels and consonants in a string

Here, we will do

  1. Create a sample string variable
  2. Separate vowels from a string
  3. Make vowels array into comma separate string
  4. Remove commas from comma-separate vowels
  5. Separate consonants from string
  6. Make consonants array into comma separate string
  7. Remove commas from comma-separate consonants
1// Create a sample string variable
2const str = "infinitbility";
3
4// Separate vowels from string
5const vowels = str.match(/[aeiou]/gi);
6console.log("vowels :", vowels)
7// vowels : [ 'i', 'i', 'i', 'i', 'i' ]
8
9// Make vowels array into comma separate string
10console.log("vowels as string:", vowels.join())
11// vowels as string: i,i,i,i,i
12
13// Remove commas from comma separate vowels
14console.log("vowels as string without comma:", vowels.join().replace(/[,]/g, ""))
15// vowels as string without comma: iiiii
16
17// Separate consonants from string
18const consonants = str.match(/[^aeiou]/gi);
19console.log("consonants :", consonants)
20// consonants : [
21// 'n', 'f', 'n',
22// 't', 'b', 'l',
23// 't', 'y'
24// ]
25
26// Make consonants array into comma separate string
27console.log("consonants as string:", consonants.join())
28// consonants as string: n,f,n,t,b,l,t,y
29
30// Remove commas from comma separate consonants
31console.log("consonants as string without comma:", consonants.join().replace(/[,]/g, ""))
32// consonants as string without comma: nfntblty

Output

I hope it helps you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to store multiple strings in an array in Javascript?

An example of store multiple strings in an array javascript with the step-by-step guide.

October 17th, 2022 · 1 min read

Which method is used to lowercase all the characters in a string in Javascript?

An example of convert string all characters into lowercase with the step-by-step guide.

October 17th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]