Symbol.search
Symbol.search
はウェルノウンシンボルで、正規表現に一致する文字列内の位置を返すメソッドを指定します。この関数は String.prototype.search()
メソッドによって呼び出されます。
詳しくは、 RegExp.prototype[@@search]()
と String.prototype.search()
を参照してください。
試してみましょう
Symbol.search のプロパティ属性 |
|
---|---|
書込可能 | 不可 |
列挙可能 | 不可 |
設定可能 | 不可 |
例
独自の文字列検索
class caseInsensitiveSearch {
constructor(value) {
this.value = value.toLowerCase();
}
[Symbol.search](string) {
return string.toLowerCase().indexOf(this.value);
}
}
console.log('foobar'.search(new caseInsensitiveSearch('BaR')));
// expected output: 3
仕様書
Specification |
---|
ECMAScript Language Specification # sec-symbol.search |
ブラウザーの互換性
BCD tables only load in the browser