Definition and Usage
The search() function for strings searches for a match between a regular expression and a string, returning the index (integer) of the first match.
String indices start at 0.
Features
- This function is case-sensitive.
- Returns
-1if no match is found.
Basic Example
const str = "apple orange banana orange Banana";
/* Searches for a match between a regular expression and the string,
returning the index (integer) of the first match. */
const searchPattern = /orange/;
const firstResult = str.search(searchPattern);
console.log(firstResult); // Output: 6
/* Case-sensitive.
Example: "banana" and "Banana" are treated as different strings. */
const upperCaseBanana = /Banana/;
const banana = str.search(upperCaseBanana);
console.log(banana); // Output: 27
/* Returns -1 if no match is found. */
const hasNoSearchPattern = /mango/;
const notFoundResult = str.search(hasNoSearchPattern);
console.log(notFoundResult); // Output: -1
Pro Tip!
If you only need to find a substring that matches a simple search term, without using a regular expression, try the string indexOf() function.
Pro Tip!
If you only need to check whether a string contains a specific substring, try the string includes() function.
Pro Tip!
The string match() function uses a regular expression to find matches in a string and returns the result.
Syntax
str.search(regexp)
str is the string to which the search() function is applied.
Parameters
regexp |
|
|---|
Return Value
Searches for a match between the given regular expression and the string, returning the index (integer) of the first match.
Returns -1 if no match is found.
Parameter and Return Value Examples
When No Parameter Is Provided
If the search() function has no parameter—in other words, if no argument is passed—it returns 0.
const str = "Hello, World!";
const result = str.search();
console.log(result); // Output: 0
When the Parameter Is an Empty String ("")
If the parameter is an empty string (""), it returns 0.
const str = "Hello, World!";
const result = str.search("");
console.log(result); // Output: 0
When the Parameter Is a String with a Space (" ")
If the parameter is a space character (" "), it searches for a match with that space character.
const str = "Hello, World!";
const result = str.search(" ");
console.log(result); // Output: 6
When There Are Multiple Matches for the Parameter
If there are multiple matches for the parameter, it returns only the index of the first match in the string.
const str = "apple orange orange orange";
const pattern = /orange/;
const result = str.search(pattern);
console.log(result); // Output: 6
When the g (Global Search) Flag Is Added to Return All Matches
The g (global search) flag in a regular expression has no effect on the result of the search() function.
const str = "apple orange orange orange";
const pattern = /orange/g;
const result = str.search(pattern);
console.log(result); // Output: 6
Regular expression flags are written in the format /regex/flags. The regular expression pattern is placed in the regex part, and the flags follow in the flags part. In the example above, /orange/g indicates that the g (global search) flag has been added to the regular expression /orange/.
Regular expression flags include the following:
| Flag | Description |
|---|---|
i |
Case-insensitive flag.
Searches for the pattern without distinguishing between uppercase and lowercase letters. |
g |
Global flag.
Searches for all occurrences of the pattern within the target string. |
m |
Multiline flag.
Enables searching across multiple lines in a string. |
Note!
The g (global search) flag of a regular expression is ignored by the search() function.
Therefore, even if the flag is added, the result remains the same, and the function always returns only the index of the first match.
When There Is No Match for the Parameter
If no match is found, the function returns -1.
const str = "Hi, World!";
const pattern = /Hello/;
const result = str.search(pattern);
console.log(result); // Output: -1
Practical Examples
The search() function can be used in a limited way to find patterns in strings using regular expressions. Let's look at the following example.
Email Address Validation
const email = "example@example.com";
if (
email &&
email.trim() &&
email.search(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/) !== -1
) {
console.log("The email address is valid.");
} else {
console.log("The email address is not valid.");
}
// Output: "The email address is valid."
Code Explanation
The trim() function returns a new string with whitespace removed from both ends of a string.
Compatibility
| Method |
Desktop Chrome
|
DesktopDesktop Edge
|
Desktop Firefox
|
Safari
|
Node.js
|
|---|---|---|---|---|---|
search()
|
1 | 12 | 1 | 4 | 0.10 |
Specifications
| Specification | |
|---|---|
search()
|
ECMAScript® 2026 Language Specification #sec-string.prototype.search |