---
hide_title: false
hide_table_of_contents: false
pagination_next: null
pagination_prev: null
---
# String.prototype.search()

The **`search()`** method executes a search for a match between a regular expression and this `String` object.

## Syntax

```js
search(regexp)
```

### Parameters

- `regexp`

  - : A regular expression object, or any object that has a [`Symbol.search`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/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)`.

### Return value

The index of the first match between the regular expression and the given string, or `-1` if no match was found.

## Description

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]()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@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]()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@search).

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 `RegExp.prototype.test()` method, which returns a boolean.
- If you need the content of the matched text, use `String.prototype.match()` or `RegExp.prototype.exec()`.
