Skip to content

JavaScript string search() | Method

  • by

The javaScript string search() method searches for a specific string or regular expression. This method accepts a regular expression and returns the index of the first match in a string.

string.search(searchValue)
search(regexp)

If the search() doesn’t find any match, it returns -1.

JavaScript string search

Simple example code.

<!DOCTYPE html>
<html>
<body>
  <script>
   let text = "Mr. Right has a right side house";
   let position = text.search("Right");

   console.log(position)

   //Search case insensitive:
   let res = text.search(/right/i);
   console.log("First mathc index",res)
 </script>

</body>
</html>

Output:

JavaScript string search Method

Use RegEx to get the first occurrence of any capital letter:

let re = /[A-Z]/;
let str = 'hi There! How are you?';
let index = str.search(re);

console.log(index);//3
const p= 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

// Any character that is not a word character or whitespace
const regex = /[^\w\s]/g;

console.log(p.search(regex)); // 43

console.log(p[paragraph.search(regex)]);// "."

Search expression

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

ValueDescription
^Matches the beginning of a string. If used with a match_parameter of ‘m’, it matches the start of a line anywhere within the expression.
$Matches the end of a string. If used with a match_parameter of ‘m’, it matches the end of a line anywhere within the expression.
*Used to specify a non-matching list where you are trying to match any character except for the ones in the list.
+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.
\bMatches a word boundary
\BMatches 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.
\nn 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.
\dMatches a digit character.
\DMatches a non-digit character.
\wMatches a word character.
\WMatches a nonword character.
\sMatches a whitespace character.
\Smatches a non-whitespace character.
\tmatches a horizontal tab character.
\vmatches a vertical tab character.
\rmatches a carriage return character.
\fmatches a form feed character.
\nmatches a line feed character.
[\b]matches a backspace character.
\0matches a NUL character.
*?Matches the preceding pattern with zero or more occurrences.
+?Matches the preceding pattern of 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.

Do comment if you have any doubts or suggestions on this JS string method.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading