Symbol.search

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.

We'd love to hear more about your role and the company you work for
Please help us by answering a few questions.

Symbol.search は静的データプロパティで、ウェルノウンシンボルである Symbol.search を表します。String.prototype.search() メソッドは第一引数から、文字列内で現在のオブジェクトに一致する場所を返すメソッドを、このシンボルで探します。

詳しくは、RegExp.prototype[Symbol.search]()String.prototype.search() を参照してください。

試してみましょう

class Search1 {
  constructor(value) {
    this.value = value;
  }
  [Symbol.search](string) {
    return string.indexOf(this.value);
  }
}

console.log("foobar".search(new Search1("bar")));
// Expected output: 3

ウェルノウンシンボル Symbol.search です。

Symbol.search のプロパティ属性
書込可能不可
列挙可能不可
設定可能不可

独自の文字列検索

js
class caseInsensitiveSearch {
  constructor(value) {
    this.value = value.toLowerCase();
  }
  [Symbol.search](string) {
    return string.toLowerCase().indexOf(this.value);
  }
}

console.log("foobar".search(new caseInsensitiveSearch("BaR"))); // 3

仕様書

Specification
ECMAScript® 2025 Language Specification
# sec-symbol.search

ブラウザーの互換性

関連情報