Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to 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
JavaScript provides several built-in string methods for pattern matching that are simple to use and efficient for basic searches.
The includes() Method
The includes() method determines whether a string contains the specified characters and returns a boolean value.
stringName.includes("pattern");
The indexOf() Method
The indexOf() method returns the position of the first occurrence of a specified value in a string, or -1 if not found.
stringName.indexOf("pattern");
The startsWith() and endsWith() Methods
These methods check if a string begins or ends with a specific substring, returning boolean values.
stringName.startsWith("character");
stringName.endsWith("character");
Example
The following example demonstrates how to use string methods to search for patterns:
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}`);
// Case sensitivity demonstration
console.log(`Includes "WORLD": ${text.includes("WORLD")}`);
Includes "welcome": true Index of "world": 22 Starts with "Hello": true Ends with "JavaScript!": true Includes "WORLD": false
Using Regular Expressions
A regular expression (RegExp) in JavaScript is an object that describes a pattern of characters. Regular expressions provide more powerful pattern matching capabilities than basic string methods.
The match() Method
The match() method returns an array of matches or null if no matches are found.
string.match(expression)
The search() Method
The search() method returns the index of the first match, or -1 if no match is found.
string.search(pattern)
Example with Regular Expressions
The following example demonstrates pattern searching using regular expressions with various flags and patterns:
const text = "Hello, welcome to the world of JavaScript! Welcome back.";
// Using search() - case sensitive
const searchPattern = /world/;
const searchIndex = text.search(searchPattern);
console.log(`Index of "world": ${searchIndex}`);
// Using match() - case insensitive
const matchPattern = /welcome/i;
const matches = text.match(matchPattern);
console.log(`First match for "welcome": ${matches ? matches[0] : 'No match found'}`);
// Using match() with global flag
const globalPattern = /welcome/gi;
const allMatches = text.match(globalPattern);
console.log(`All matches for "welcome": ${allMatches ? allMatches.join(', ') : 'No matches found'}`);
// Pattern matching with numbers
const numberText = "The year is 2024 and temperature is 25 degrees";
const numberPattern = /\d+/g;
const numbers = numberText.match(numberPattern);
console.log(`Numbers found: ${numbers ? numbers.join(', ') : 'No numbers found'}`);
Index of "world": 22 First match for "welcome": welcome All matches for "welcome": welcome, Welcome Numbers found: 2024, 25
Comparison of Methods
| Method | Return Type | Case Sensitive | Use Case |
|---|---|---|---|
includes() |
Boolean | Yes | Simple presence check |
indexOf() |
Number | Yes | Find position |
match() |
Array/null | Configurable | Complex patterns |
search() |
Number | Configurable | Pattern position |
Conclusion
JavaScript provides multiple approaches for string pattern searching. Use built-in string methods for simple searches and regular expressions for complex pattern matching. Choose the method that best fits your specific requirements for optimal performance and code clarity.
