JavaScript Search within String using the search() and match() method

In Javascript, filtering and searching methods are often used to display desired output to the user or to search from string and then match them to a searched string. In real projects, we use them in searching through rows in a table which will return us rows that have matched our given search string.

In this post let us look at examples of matching patterns in a string.

Table of Contents

Introduction

Javascript provides string methods for searching and matching strings these methods can take strings to be searched as parameters or regular expressions can be used for retrieving more results.
Let us look at examples using string methods .search() and .match().

search() the method returns the position of string match to the given pattern.

let sentence = "If you wait too long for the perfect moment, the perfect moment will pass you by";

sentence = `/${sentence}/gmi`;

let search_pattern = "will pass";
let search_result = sentence.search(search_pattern);

if(search_result>=0){
    console.log("Searched Pattern is found at position : "+search_result);  
}else{
    console.log("Searched Pattern not found.");  
}

//Console Output
Searched Pattern is found at position : 65
Javascript Search word in string and get its position
Javascript Search word in a string and get its position

In the above example sentence is a string and search_pattern is the pattern we are searching inside a string.
In this example, we are using regular expressions. The variable search_result returns the first position of matched pattern encountered.

Search which returns matched words using string match() method

Suppose in cases where we want to return all those matched patterns then it is possible using string.match() method.

let sentence = "If you wait too long for the perfect moment, the perfect moment will pass you by";
let pattern = /the/gi;
let result = sentence.match(pattern);
console.log(result);

//Console Output
(2) ["the", "the"]
Javascript search string using match() method
Javascript search string using match() method

Variable pattern which searches for a word this in string sentence returns the matched output.

And what if we need to search for multiple patterns in variable sentence then we use the vertical line | which is an or operator.

let pattern = /the|wait/gi; //with multiple words match
let sentence = "If you wait too long for the perfect moment, the perfect moment will pass you by";
let result = sentence.match(pattern);
console.log(result);

//Console Output
(3) ["wait", "the", "the"]
Javasript multiple words searching and display in an array.
Javascript multiple words searching and display in an array.

Conclusion

We have come to the end of our post on JavaScript Search within String using the search() and match() method. If you like our content then please do share it and comment to clarify your doubts.

Related Posts

Summary
Review Date
Reviewed Item
JavaScript Search within String using the search() and match() method
Author Rating
51star1star1star1star1star
Software Name
Javascript Programming
Software Name
Windows Os, Mac Os, Ubuntu Os
Software Category
Web Development