How to useString match() Method in Javascript

The match() method is used to search for and extract substrings from a string based on a specified pattern. It allows you to perform powerful string matching using regular expressions or simple string patterns.

Syntax:

string.match(char)

Example: In this example, we are using the match() method to find the index of G from our given string.

Javascript
let str = "Welcome to w3wiki";
let char = "G";
let index = str.match(char);
console.log(index.index);

Output
11

How to Find the Index of Specific Character in a String in JavaScript ?

We will see how to find the index of a specific character in a string using JavaScript. A string is a sequence of characters, such as letters, numbers, or symbols, enclosed in single or double quotes. Here we are going to find the index of a specific character from our given string.

Several approaches can be used to find the index of a specific character in a string using JavaScript, which are listed below:

Table of Content

  • Approach 1: Using String indexOf() Method
  • Approach 2: Using String search() Method
  • Approach 3: Using String match() Method
  • Approach 4: Using String lastIndexOf() Method
  • Approach 5: Using Lodash _.indexOf() Method
  • Approach 6: Using a for loop
  • Approach 7: Using ES6 findIndex() Method with Array.from()

Similar Reads

Approach 1: Using String indexOf() Method

The indexOf() method is a method to find the index of the first occurrence of a specific character within a string. It takes the character as an argument and returns the index of its first appearance....

Approach 2: Using String search() Method

The search() method is a powerful tool used to search for a specific substring within a string. It returns the index of the first occurrence of the specified substring....

Approach 3: Using String match() Method

The match() method is used to search for and extract substrings from a string based on a specified pattern. It allows you to perform powerful string matching using regular expressions or simple string patterns....

Approach 4: Using String lastIndexOf() Method

The lastIndexOf() method finds the index of the last occurrence of a specific character in a string. It works similarly to indexOf() but searches from the end of the string....

Approach 5: Using Lodash _.indexOf() Method

lodash _.indexOf() method is used to get the index of the first occurrence of the particular element in the array. If fromIndex is not present in the array negative one is given as the output and no error is displayed....

Approach 6: Using a for loop

In this approach, we can iterate over each character in the string using a for loop and check if it matches the specific character we are looking for. If a match is found, we return the index of that character. If no match is found, we return -1 to indicate that the character is not present in the string....

Approach 7: Using ES6 findIndex() Method with Array.from()

In this approach, we can utilize ES6 features like Array.from() along with the findIndex() method to find the index of a specific character in a string. This method converts the string into an array of characters, then applies the findIndex() method to search for the desired character....

Contact Us