Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript Program to Match Single Character with Multiple Possibilities
Next article icon

JavaScript Program to Match Single Character with Multiple Possibilities

Last Updated : 27 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
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.

Table of Content

  • Using JavaScript RegExp test() Method
  • Using JavaScript String match() Method
  • Using JavaScript String includes() Method
  • Using JavaScript String indexOf() Method
  • Using Array.some()

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' ] 

Using Array.some()

Using Array.some(), convert the string to an array of characters, then check if any specified characters exist in the array. If any character matches, some() returns true; otherwise, it returns false.

Example: In this example The function matchSingleCharacter checks if any character in str exists in the possibilities array, returning true if there's a match, else false.

JavaScript
function matchSingleCharacter(str, possibilities) {   return [...str].some(char => possibilities.includes(char)); }   console.log(matchSingleCharacter("hello", ['a', 'e', 'i', 'o', 'u']));  

Output
true 



Next Article
JavaScript Program to Match Single Character with Multiple Possibilities

J

jatinsharmatu54
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-string
  • JavaScript-DSA
  • JavaScript-Program

Similar Reads

    JavaScript Program to Find Missing Characters to Make a String Pangram
    We have given an input string and we need to find all the characters that are missing from the input string. We have to print all the output in the alphabetic order using JavaScript language. Below we have added the examples for better understanding. Examples: Input : welcome to geeksforgeeksOutput
    6 min read
    JavaScript Program to Search a Word in a 2D Grid of Characters
    In this article, we will solve a problem in which you are given a grid, with characters arranged in a two-layout(2D). You need to check whether a given word is present in the grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match
    7 min read
    JavaScript Program to Validate String for Uppercase, Lowercase, Special Characters, and Numbers
    In this article, we are going to learn how can we check if a string contains uppercase, lowercase, special characters, and numeric values. We have given string str of length N, the task is to check whether the given string contains uppercase alphabets, lowercase alphabets, special characters, and nu
    4 min read
    Javascript Program to Check if a given string is Pangram or not
    In this article, we are going to implement algorithms to check whether a given string is a pangram or not. Pangram strings are those strings that contain all the English alphabets in them.Example: Input: “Five or six big jet planes zoomed quickly by tower.” Output: is a Pangram Explanation: Does'not
    7 min read
    Wildcard Pattern Matching in JavaScript
    In this article, we will see Pattern matching with wildcards which is an encountered problem, in the field of computer science and string manipulation. The objective is to determine whether a given wildcard pattern matches a string or not. In this article, we will discuss the step-by-step algorithm
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences