Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1040790 of RegExp.prototype[@@search]()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/RegExp/@@search
  • Revision title: RegExp.prototype[@@search]()
  • Revision id: 1040790
  • Created:
  • Creator: rolfedh
  • Is current revision? No
  • Comment Editorial review. No changes needed.

Revision Content

{{JSRef}}

The [@@search]() method executes a search for a match between a this regular expression and a string.

Syntax

regexp[Symbol.search](str)

Parameters

str
A {{jsxref("String")}} that is a target of the search.

Return value

integer
If successful, [@@search]() returns the index of the first match of the regular expression inside the string. Otherwise, it returns -1.

Description

This method is called internally in {{jsxref("String.prototype.search()")}}. For example, following 2 code returns same result, and 1st case calls 2nd case internally.

'abc'.search(/a/);

/a/[Symbol.search]('abc');

This method exists for customizing search behavior in RegExp subclass.

Examples

Call directly

This method can be used almost same way as {{jsxref("String.prototype.search()")}}, except different this and arguments order.

var re = /-/g;
var str = '2016-01-02';
var result = re[Symbol.search](str);
console.log(result);  // 4

Use in subclass

Subclass of {{jsxref("RegExp")}} can override [@@search]() method to modify the behavior.

class MyRegExp extends RegExp {
  constructor(str) {
    super(str)
    this.pattern = str;
  }
  [Symbol.search](str) {
    return str.indexOf(this.pattern);
  }
}

var re = new MyRegExp('a+b');
var str = 'ab a+b';
var result = str.search(re); // String.prototype.search calls re[@@search].
console.log(result); // 3

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-regexp.prototype-@@search', 'RegExp.prototype[@@search]')}} {{Spec2('ES6')}}  
{{SpecName('ESDraft', '#sec-regexp.prototype-@@search', 'RegExp.prototype[@@search]')}} {{Spec2('ESDraft')}}  

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatUnknown}} {{CompatGeckoDesktop(48)}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatUnknown}} {{CompatGeckoMobile(48)}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

See also

  • {{jsxref("String.prototype.search()")}}
  • {{jsxref("RegExp.prototype.@@match()", "RegExp.prototype[@@match]()")}}
  • {{jsxref("RegExp.prototype.@@replace()", "RegExp.prototype[@@replace]()")}}
  • {{jsxref("RegExp.prototype.@@split()", "RegExp.prototype[@@split]()")}}
  • {{jsxref("RegExp.prototype.exec()")}}
  • {{jsxref("RegExp.prototype.test()")}}

Revision Source

<div>{{JSRef}}</div>

<p>The <strong><code>[@@search]()</code></strong> method executes a search for a match between a <code>this</code> regular expression and a string.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
<var>regexp</var>[Symbol.search](str)</pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt><code>str</code></dt>
 <dd>A {{jsxref("String")}} that is a target of the search.</dd>
</dl>

<h3 id="Return_value">Return value</h3>

<dl>
 <dt>integer</dt>
 <dd>If successful, <code>[@@search]()</code> returns the index of the first match of the regular expression inside the string. Otherwise, it returns -1.</dd>
</dl>

<h2 id="Description">Description</h2>

<p>This method is called internally in {{jsxref("String.prototype.search()")}}. For example, following 2 code returns same result, and 1st case calls 2nd case internally.</p>

<pre class="brush: js">
'abc'.search(/a/);

/a/[Symbol.search]('abc');</pre>

<p>This method exists for customizing search behavior in RegExp subclass.</p>

<h2 id="Examples">Examples</h2>

<h3 id="Call_directly">Call directly</h3>

<p>This method can be used almost same way as {{jsxref("String.prototype.search()")}}, except different <code>this</code> and arguments order.</p>

<pre class="brush: js">
var re = /-/g;
var str = '2016-01-02';
var result = re[Symbol.search](str);
console.log(result);  // 4
</pre>

<h3 id="Use_in_subclass">Use in subclass</h3>

<p>Subclass of {{jsxref("RegExp")}} can override <code>[@@search]()</code> method to modify the behavior.</p>

<pre class="brush: js">
class MyRegExp extends RegExp {
  constructor(str) {
    super(str)
    this.pattern = str;
  }
  [Symbol.search](str) {
    return str.indexOf(this.pattern);
  }
}

var re = new MyRegExp('a+b');
var str = 'ab a+b';
var result = str.search(re); // String.prototype.search calls re[@@search].
console.log(result); // 3
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-regexp.prototype-@@search', 'RegExp.prototype[@@search]')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-regexp.prototype-@@search', 'RegExp.prototype[@@search]')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop(48)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile(48)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("String.prototype.search()")}}</li>
 <li>{{jsxref("RegExp.prototype.@@match()", "RegExp.prototype[@@match]()")}}</li>
 <li>{{jsxref("RegExp.prototype.@@replace()", "RegExp.prototype[@@replace]()")}}</li>
 <li>{{jsxref("RegExp.prototype.@@split()", "RegExp.prototype[@@split]()")}}</li>
 <li>{{jsxref("RegExp.prototype.exec()")}}</li>
 <li>{{jsxref("RegExp.prototype.test()")}}</li>
</ul>
Revert to this revision