Opens in a new tab
A picture of Jeff Brigman
I CODE TODAY FOR
A BETTER TOMORROW

Glossary Term

RegExp.prototype[Symbol.search]()

2 tags 1 category

The Symbol.search method of RegExp instances specifies how String.prototype.search should behave.

This method exists for customizing the search behavior in RegExp subclasses. It is called internally in String.prototype.search(). For example, the following two examples return the same result.

Symbol.search always calls the regex’s exec() method exactly once, and returns the index property of the result, or -1 if the result is null. The g flag has no effect with this method.

This method does not copy the regular expression, unlike [Symbol.split](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/Symbol.split) or [Symbol.matchAll](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/Symbol.matchAll). However, unlike [Symbol.match](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/Symbol.match) or [Symbol.replace](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/Symbol.replace), it will always set lastIndex to 0 when execution starts and restore it to the previous value when it exits, therefore generally avoiding side effects. This means that it always returns the first match in the string even when lastIndex is non-zero, and sticky regexes always search strictly at the beginning of the string.

Syntax example: regexp[Symbol.search](str)

Term Connections

Explore related terms connected by shared tags, shared categories, and relationship phrases. This map loads only when it comes into view.

Loading term connections…