`search()` 方法执行正则表达式和 `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`
  一个`正则表达式（regular expression）`对象。如果传入一个非正则表达式对象 `obj`，则会使用 `new RegExp(obj)` 隐式地将其转换为正则表达式对象。

### 返回值

如果匹配成功，则 `search()` 返回正则表达式在字符串中首次匹配项的索引;否则，返回 -1。

## 描述

当你想要知道字符串中是否存在某个模式（pattern）时可使用 `search()`，类似于正则表达式的 `test()` 方法。当要了解更多匹配信息时，可使用 `match()`（但会更慢一些），该方法类似于正则表达式的 `exec()` 方法。

## 示例

### 例子：使用 `search()`

The following example searches a string with 2 different regex objects to show a successful search (positive value) vs. an unsuccessful search (-1)

    var str = "hey JudE";
    var re = /[A-Z]/g;
    var re2 = /[.]/g;
    console.log(str.search(re)); // returns 4, which is the index of the first capital letter "J"
    console.log(str.search(re2)); // returns -1 cannot find '.' dot punctuation

## 规范

规范 | 状态 | 备注
--- | --- | ---
ECMAScript 3rd Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.2.
ECMAScript 5.1 (ECMA-262) String.prototype.search | Standard |
ECMAScript 2015 (6th Edition, ECMA-262) String.prototype.search | Standard |
ECMAScript Latest Draft (ECMA-262) String.prototype.search | Draft |

## 浏览器兼容性

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.

Update compatibility data on GitHub

| Desktop | Mobile | Server
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | Safari on iOS | Samsung Internet | Node.js
`search` | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes
`flags` Deprecated Non-standard | Chrome No support No | Edge No support No | Firefox No support 1 — 49 | IE No support No | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Edge Mobile No support No | Firefox Android No support 4 — 49 | Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No | nodejs No support No

### Legend

Full support
  Full support
No support
  No support
Non-standard. Expect poor cross-browser support.
  Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
  Deprecated. Not for use in new websites.

## Gecko 注意事项

- 在 Gecko 8.0 及之前的版本，`search()` 的实现有问题：在调用该方法时，若不传递参数或者参数为 `undefined`，该方法将不会匹配空字符串，而是匹配字符串 "undefined"。这个问题已被修复，现在，`"a".search()` 和 `"a".search(undefined)` 都能正确返回 0。
- Starting with Gecko 39 (Firefox 39 / Thunderbird 39 / SeaMonkey 2.36), the non-standard `flags` argument is deprecated and throws a console warning (bug 1142351).
- Starting with Gecko 47 (Firefox 47 / Thunderbird 47 / SeaMonkey 2.44), the non-standard `flags` argument is no longer supported in non-release builds and will soon be removed entirely (bug 1245801).
- Starting with Gecko 49 (Firefox 49 / Thunderbird 49 / SeaMonkey 2.46), the non-standard `flags` argument is no longer supported (bug 1108382).

## 参见

- `String.prototype.match()`
- `RegExp.prototype.exec()`