In this article, we are going to learn the string search method in JavaScript. A string is a sequence of characters used to represent text or data, strings can be enclosed within single or double quotes. here we are using some common approaches to search for a particular string/character from our given string.
There are several approaches that can be used to search strings, which are listed below:
- Using search() Method
- Using match() Method
- Using the includes() Method
- Using startsWith() Method
- Using endsWith() Method
- Using lastIndexOf() Method
We will explore all the above methods along with their basic implementation with the help of examples.
Approach 1: Using the search() Method
In this approach, we are using the search() method, this method searches for a substring in a string and returns the index of the first occurrence, or -1 if not found.
Syntax:
str1.search( A )
Example: In this example, we are using the search() method to search computer words from our given string.
Javascript
let str1 = "neveropen, A computer science portal" ; let result = str1.search( "computer" ); console.log(result); |
17
Approach 2: Using match() Method
The match() method is an inbuilt function in JavaScript used to search a string for a match against any regular expression. it will return in array form.
Syntax:
str1.match( /word/g )
Example:
Javascript
let str1 = "neveropen, A computer science portal" ; let result = str1.match(/computer/g); console.log(result); |
[ 'computer' ]
Approach 3: Using the includes() Method
The includes() method checks if a string contains a specific substring, returning true if found, false if not.
Syntax:
str1.includes( "word" )
Example: In this example, we are using the includes() method to find the specific word from our given string.
Javascript
let str1 = "neveropen, A computer science portal" ; let result = str1.includes( "science" ); console.log(result); |
true
Approach 4: Using startsWith() Method
The startsWith() method checks if a string starts with a specified substring, returning true if it matches, false otherwise.
Syntax:
str1.startsWith( "word" )
Example: In this example, we are using the startsWith() method to find our specific word from our given string.
Javascript
let str1 = "neveropen, A computer science portal" ; let result = str1.startsWith( "neveropen" ); console.log(result); |
true
Approach 5: Using endsWith() Method
The endsWith() method checks if a string ends with a specified substring, returning true if it matches, false otherwise.
Syntax:
str1.endsWith( "word" )
Example: In this example, we are using endsWith() to find the last word from our given string
Javascript
let str1 = "neveropen, A computer science portal" ; let result = str1.endsWith( "portal" ); console.log(result); |
true
Approach 6: Using lastIndexOf() Method
The lastIndexOf() method searches for the last occurrence of a substring in a string and returns its index or -1 if not found.
Syntax:
str1.lastIndexOf( "word" )
Example: In this example, we are using the lastIndexOf() method to find the index of a given string.
Javascript
let str1 = "neveropen, A computer science portal" ; let result = str1.lastIndexOf( "A" ); console.log(result); |
15