How to use regular expression In Javascript

In this method, we will create a regular expression for the vowels and test if the given input char satisfy the regex condition.

Example: In this example, we will implement regex for the vowels and output the result.

Javascript
function checkChar(char){
    ch  = char.toLowerCase(); 
    const regex = /^[aeiou]$/i;
    if(regex.test(ch))
    return console.log("Given character is a Vowel");
    return console.log("Given character is a Consonent");
}


checkChar('I');
checkChar('Z');

Output
Given character is a Vowel
Given character is a Consonent

JavaScript Program to Find if a Character is a Vowel or Consonant

In this article, we will see different approaches to finding whether a character is a vowel or a consonant using JavaScript. We will check the condition of a character being Vowel and display the result.

Similar Reads

Approaches to find if a character is a vowel or consonant

Table of Content Using conditional statementsUsing JavaScript array and Array .includes() methodUsing JavaScript regular expressionUsing SetUsing Switch Statement...

1. Using conditional statements

In this method, we will use if-else conditional statements to check if the letter is a vowel or not and display the output result....

2. Using JavaScript array and Array .includes() method

In this method, we will use JavaScript array of vowels and use includes method to check if given character is present in the array then it is a vowel and consonant otherwise....

3. Using JavaScript regular expression

In this method, we will create a regular expression for the vowels and test if the given input char satisfy the regex condition....

4. Using Set

Using a Set, vowels are stored efficiently for quick lookup. If the character is in the set, it’s a vowel; otherwise, it’s a consonant if it’s a letter, or neither. This ensures O(1) time complexity for the check....

5. Using Switch Statement

In this approach, we can utilize a switch statement to check if the character is a vowel or a consonant. We can define cases for each vowel and a default case for consonants....