How to useJavaScript String indexOf() Method in Javascript
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.
// 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' ]
JavaScript Program to Match Single Character with Multiple Possibilities
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()