Open In App

JavaScript Program to Match Single Character with Multiple Possibilities

Last Updated : 22 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will write programs to check all possible matching in JavaScript. We will check the occurrence of characters individually in the given string or sequence. In this article, we will check the occurrence of vowels in the given string.

Approach 1: Using JavaScript RegExp test() Method

The RegExp test() Method in JavaScript is used to test for the match in a string. If there is a match this method returns true else it returns false.

Syntax: 

RegExpObject.test( str )

Example: This example illustrates matching a single character with Multiple possibilities using the RegExp test() Method.

Javascript




// Create input string
let string = "GeeksforGeeks";
  
// Create global regular expression 
// containing letters to be matched
let regx = /[aeiou]/g;
  
// Store results from RegX test method
let result = regx.test(string);
  
// Display the result
console.log(result);


Output

true

Approach 2: Using JavaScript String match() Method

The JavaScript String match() method is an inbuilt function in JavaScript used to search a string for a match against any regular expression. If the match is found, then this will return the match as an array. 

Syntax: 

string.match( regExp );

Example: This example illustrates matching a single character with Multiple possibilities using the String match() Method

Javascript




// Create input string
let string = "GeeksforGeeks";
  
// Create global regular expression 
// containing letters to be matched
let regx = /[aeiou]/g;
  
// Store results from string match method
let result = [...string.match(regx)];
  
// Display the result
console.log(result);


Output

[ 'e', 'e', 'o', 'e', 'e' ]

Approach 3: Using JavaScript String includes() Method

JavaScript String includes() method determines whether a string contains the given characters within it or not. This method returns true if the string contains the characters, otherwise, it returns false.

Syntax:  

string.includes(searchvalue, start);

Example: This example illustrates matching a single character with Multiple possibilities using the includes() Method.

Javascript




// Create a input string
let string = "GeeksforGeeks";
  
// The letters to be matched
const character = "aeiou";
  
// To store the letters found
result = [];
  
// Split character string and iterate
character.split("").some((char) => {
  
    // Check if character is present
    if (string.includes(char))
  
        // Store the characters
        result.push(char);
});
  
// Display the result
console.log(result);


Output

[ 'e', 'o' ]

Approach 4: Using JavaScript String indexOf() Method

The JavaScript String indexOf() method finds the index of the first occurrence of the argument string in the given string. The value returned is 0-based.

Syntax:

str.indexOf(searchValue , index);

Example: This example illustrates matching a single character with Multiple possibilities using the String indexOf() Method.

Javascript




// Create a input string
let string = "JavaScript";
  
// The letters to be matched
const character = "aeiou";
  
// To store the letters found
result = [];
  
// Iterate the characters to apply
// indexOf at each one
Array.from(character).some((char) => {
    if (string.indexOf(char) !== -1)
      
        // Store the found character
        result.push(char);
});
  
// Display the result
console.log(result);


Output

[ 'a', 'i' ]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads