Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To find the first vowel in a string in javascript, use the /[aeiou]/gi regex with the string match method it will return an array and the first index of the array is the first vowel of the string.

Today, I’m going to show you How do I find the first vowel in a string in javascript, as mentioned above, I’m going to take a sample string using the match() method with regex to get the first vowel.

Let’s start today’s tutorial on how do you find the first vowel in a string in javascript.

Javascript finds the first vowel in a string

Here, we will do

  1. Create a sample string variable with data.
  2. Use the string match method with regex
  3. check array length and take 0’th index value for the first vowel of the string
// Create sample string variable with data.
let str = "infintbility";

// Use string match method with regex
const vowels = str.match(/[aeiou]/gi); 

// check array length and take 0'th index value for first vowel of string
if(vowels.length > 0){
 console.log("First vowel is", vowels[0])
} else {
 console.log("String haven't any vowels")
}

Output

I hope it helps you, All the best đź‘Ť.