search()
method executes a search for a match between a regular expression and this
String
对象。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
str.search(regexp)
regexp
regexp
is passed, it is implicitly converted to a
RegExp
with
new RegExp(
regexp
)
.
The index of the first match between the regular expression and the given string, or
-1
if no match was found.
When you want to know whether a pattern is found, and
also
know its index within a string, use
search()
. (If you only want to know if it exists, use the similar
test()
method on the
RegExp
prototype, which returns a boolean.)
For more information (but slower execution) use
match()
(similar to the regular expression
exec()
方法)。
The following example searches a string with two different regex objects to show a successful search (positive value) vs. an unsuccessful search (
-1
)
let str = "hey JudE" let re = /[A-Z]/g let reDot = /[.]/g 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
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'String.prototype.search' in that specification. |
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
更新 GitHub 上的兼容性数据| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
search
|
Chrome 1 | Edge 12 | Firefox 1 | IE 4 | Opera 4 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
flags
弃用
非标
|
Chrome No | Edge No | Firefox 1 — 49 | IE No | Opera No | Safari No | WebView Android No | Chrome Android No | Firefox Android 4 — 49 | Opera Android No | Safari iOS No | Samsung Internet Android No | nodejs No |
完整支持
不支持
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
String
String.fromCharCode()
String.fromCodePoint()
String.prototype.anchor()
String.prototype.big()
String.prototype.blink()
String.prototype.bold()
String.prototype.charAt()
String.prototype.charCodeAt()
String.prototype.codePointAt()
String.prototype.concat()
String.prototype.endsWith()
String.prototype.fixed()
String.prototype.fontcolor()
String.prototype.fontsize()
String.prototype.includes()
String.prototype.indexOf()
String.prototype.italics()
String.prototype.lastIndexOf()
String.prototype.link()
String.prototype.localeCompare()
String.prototype.match()
String.prototype.matchAll()
String.prototype.normalize()
String.prototype.padEnd()
String.prototype.padStart()
String.prototype.repeat()
String.prototype.replace()
String.prototype.replaceAll()
String.prototype.search()
String.prototype.slice()
String.prototype.small()
String.prototype.split()
String.prototype.startsWith()
String.prototype.strike()
String.prototype.sub()
String.prototype.substr()
String.prototype.substring()
String.prototype.sup()
String.prototype.toLocaleLowerCase()
String.prototype.toLocaleUpperCase()
String.prototype.toLowerCase()
String.prototype.toSource()
String.prototype.toString()
String.prototype.toUpperCase()
String.prototype.trim()
String.prototype.trimEnd()
String.prototype.trimStart()
String.prototype.valueOf()
String.prototype[@@iterator]()
String.raw()
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()