Symbol.search

Symbol.search 静态数据属性代表 众所周知的符号 @@searchString.prototype.search() 方法在其第一个参数上查找此符号,以查找返回与当前对象匹配的字符串中的索引的方法。

¥The Symbol.search static data property represents the well-known symbol @@search. The String.prototype.search() method looks up this symbol on its first argument for the method that returns the index within a string that matches the current object.

有关详细信息,请参阅 RegExp.prototype[@@search]()String.prototype.search()

¥For more information, see RegExp.prototype[@@search]() and String.prototype.search().

Try it

¥Value

众所周知的符号 @@search

¥The well-known symbol @@search.

Property attributes of Symbol.search
Writable no
Enumerable no
Configurable no

示例

¥Examples

自定义字符串搜索

¥Custom string 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 Language Specification
# sec-symbol.search

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看