totn JavaScript

JavaScript: String search() method

This JavaScript tutorial explains how to use the string method called search() with syntax and examples.

Description

In JavaScript, search() is a string method that is used to search for a specific string or regular expression. Because the search() method is a method of the String object, it must be invoked through a particular instance of the String class.

Syntax

In JavaScript, the syntax for the search() method is:

string.search(search_expression);

Parameters or Arguments

search_expression

It is either a string value or a RegExp object that will be searched for in string. As a RegExp object, it can be a combination of the following:

Value Description
^ Matches the beginning of a string. If used with a match_parameter of 'm', it matches the start of a line anywhere within expression.
$ Matches the end of a string. If used with a match_parameter of 'm', it matches the end of a line anywhere within expression.
* Matches zero or more occurrences.
+ Matches one or more occurrences.
? Matches zero or one occurrence.
. Matches any character except NULL.
| Used like an "OR" to specify more than one alternative.
[ ] Used to specify a matching list where you are trying to match any one of the characters in the list.
[^ ] Used to specify a nonmatching list where you are trying to match any character except for the ones in the list.
( ) Used to group expressions as a subexpression.
\b Matches a word boundary
\B Matches a non-word boundary
{m} Matches m times.
{m,} Matches at least m times.
{m,n} Matches at least m times, but no more than n times.
\n n is a number between 1 and 9. Matches the nth subexpression found within ( ) before encountering \n.
[..] Matches one collation element that can be more than one character.
[::] Matches character classes.
[==] Matches equivalence classes.
\d Matches a digit character.
\D Matches a nondigit character.
\w Matches a word character.
\W Matches a nonword character.
\s Matches a whitespace character.
\S matches a non-whitespace character.
\t matches a horizontal tab character.
\v matches a vertical tab character.
\r matches a carriage return character.
\f matches a form feed character.
\n matches a line feed character.
[\b] matches a backspace character.
\0 matches a NUL character.
*? Matches the preceding pattern zero or more occurrences.
+? Matches the preceding pattern one or more occurrences.
?? Matches the preceding pattern zero or one occurrence.
{n}? Matches the preceding pattern n times.
{n,}? Matches the preceding pattern at least n times.
{n,m}? Matches the preceding pattern at least n times, but not more than m times.

Returns

The search() method returns an integer representing the position of the first character of search_expression found in string. The first position in string is 0 and the last position in string is string.length-1.

If the search_expression is not found in string, the search() method will return -1.

Note

  • The search() method does not change the value of the original string.
  • The search() method performs a case-sensitive search.
  • The search() method does not support the g attribute when a regular expression is provided and therefore can not perform global matches. This means that the search() method only finds the first match and can not find all matches in a string.
  • When the search() method finds a match, it sets RegExp.leftContext, RegExp.rightContext and RegExp.$1 which are properties of the RegExp class. These properties provide additional information about the match found.

Example

Let's take a look at an example of how to use the search() method in JavaScript.

String as the Search Expression

The simplest way to use the search() method is to search for a string and does not involve regular expression objects.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.search('T'));
console.log(totn_string.search('t'));

In this example, we have declared a variable called totn_string that is assigned the string value of 'TechOnTheNet'. We have then invoked the search() method of the totn_string to find a string value.

We have written the output of the search() method to the web browser console log, for demonstration purposes, to show what the search() method returns.

The following will be output to the web browser console log:

0
11

In this example, the search() method performed a case-sensitive search for the first occurrence of a string value. As you can see, the search() method returned 0 when it was searching for 'T' which is the first character in the string 'TechOnTheNet'. But the search() method returned 11 when searching for 't' which is the last character in the string.

Regular Expression as the Search Expression

Next, we'll take a look at how to search using a regular expression. You can use the search() method to search for a regular expression pattern.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.search(/[A-Z]/));

The following will be output to the web browser console log:

0

In this example, the search() method performed a search for the first occurrence of an uppercase character and returned a value of 0. This return value is the position of 'T' in the string 'TechOnTheNet'.