![]() |
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 Approach 1: Using JavaScript RegExp test() MethodThe 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.
Output true Approach 2: Using JavaScript String match() MethodThe 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
Output [ 'e', 'e', 'o', 'e', 'e' ] Approach 3: Using JavaScript String includes() MethodJavaScript 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.
Output [ 'e', 'o' ] Approach 4: Using JavaScript String indexOf() MethodThe 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.
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.
Output true |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |