
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Search a String for a Pattern in JavaScript
Searching strings for specific patterns is a common task in JavaScript. Strings are data types and are represented as a sequence of characters. Searching patterns means verifying the presence of a specific pattern in a given string. A pattern would be a specific word, specific characters, and so on.
JavaScript offers several powerful methods for pattern matching, such as inbuilt string methods and regular expressions. This article will guide you on how to search strings for specific patterns in JavaScript.
Table of contents
Searching strings for specific patterns in JavaScript can be done in the following ways:
Using Inbuilt String Search Methods
Searching strings for a specific pattern can be simply implemented using inbuilt JavaScript methods.
- includes(): The includes() method determines whether a string contains the specified characters. It returns the boolean value. The following is a syntax for this:
stringName.includes("pattern");
stringName.indexOf("pattern");
stringName.startsWith("character"); const isHtml = stringName.endsWith("character");
Example
The following uses string includes(), indexOf(), startWith(), and endWith() methods to search a string for a pattern in JavaScript.
const text = "Hello, welcome to the world of JavaScript!"; // Using includes() const hasWelcome = text.includes("welcome"); console.log(`Includes "welcome": ${hasWelcome}`); // Using indexOf() const index = text.indexOf("world"); console.log(`Index of "world": ${index}`); // Using startsWith() const startsWithHello = text.startsWith("Hello"); console.log(`Starts with "Hello": ${startsWithHello}`); // Using endsWith() const endsWithScript = text.endsWith("JavaScript!"); console.log(`Ends with "JavaScript!": ${endsWithScript}`);
Output
Includes "welcome": true Index of "world": 22 Starts with "Hello": true Ends with "JavaScript!": true
Using Regular Expressions
A regular expression (RegExp) in JavaScript is an object that describes a pattern of characters. It can contain the alphabetical, numeric, and special characters. To search strings for a specific pattern, regular expressions can be used.
- String.match(): The match() method is one of the regular expression methods that returns an array of matches. The following is a syntax for this:
string.match( expression )
str.search( pattern )
Example
In the following example, the code demonstrates two ways to search for patterns in a string. The search() method finds the index of the word "world" in the text and logs it, while the match() method checks for the presence of the word "welcome" and logs the match if found, or indicates that no match was found.
const text = "Hello, welcome to the world of JavaScript!"; // Using search() const searchPattern = /world/; const searchIndex = text.search(searchPattern); console.log(`Index of "world": ${searchIndex}`); // Using match() const matchPattern = /welcome/; const matches = text.match(matchPattern); console.log(`Matches for "welcome": ${matches ? matches[0] : 'No match found'}`);
Output
Index of "world": 22 Matches for "welcome": welcome
Conclusion
In this article, we have seen various methods for searching strings for a pattern. JavaScript has strong tools for searching strings and finding patterns in your applications. By picking the right method for what you need, you can easily search and work with text data in different situations.