The search()
method of String
values executes a search for a match between a regular expression and this string, returning the index of the first match in the string.
The search()
method of String
values executes a search for a match between a regular expression and this string, returning the index of the first match in the string.
search(regexp)
regexp
A regular expression object, or any object that has a Symbol.search
method.
If regexp
is not a RegExp
object and does not have a Symbol.search
method, it is implicitly converted to a RegExp
by using new RegExp(regexp)
.
The index of the first match between the regular expression and the given string, or -1
if no match was found.
The implementation of String.prototype.search()
itself is very simple — it simply calls the Symbol.search
method of the argument with the string as the first parameter. The actual implementation comes from RegExp.prototype[@@search]()
.
The g
flag of regexp
has no effect on the search()
result, and the search always happens as if the regex's lastIndex
is 0. For more information on the behavior of search()
, see RegExp.prototype[@@search]()
.
When you want to know whether a pattern is found, and also know its index within a string, use search()
.
RegExp.prototype.test()
method, which returns a boolean.String.prototype.match()
or RegExp.prototype.exec()
.The following example searches a string with two different regex objects to show a successful search (positive value) vs. an unsuccessful search (-1
).
const str = "hey JudE"; const re = /[A-Z]/; const reDot = /[.]/; console.log(str.search(re)); // returns 4, which is the index of the first capital letter "J" console.log(str.search(reDot)); // returns -1 cannot find '.' dot punctuation
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | ||
search |
1 | 12 | 1 | 4 | 1 | 18 | 4 | 10.1 | 1 | 1.0 | 4.4 | 1.0 | 0.10.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search