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 2020年1月.
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® 2026 Language Specification> # sec-symbol.search> |
ブラウザーの互換性
Loading…