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:

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);//3const 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:
| 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 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. |
| \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 non-digit 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 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