Return the position of the first occurrence of the string passed as parameter in the current string.
返回作为参数传递的当前字符串中第一次出现的字符串的位置。
It returns the index of the start of the occurrence, or -1 if no occurrence is found.
它返回事件开始的索引,如果没有发现,则返回-1。
'JavaScript'.search('Script') //4
'JavaScript'.search('TypeScript') //-1
You can search using a regular expression (and in reality, even if you pass a string, that’s internally and transparently used as a regular expression too).
您可以使用正则表达式进行搜索(实际上,即使您传递了一个字符串,该字符串也在内部和透明地用作正则表达式)。
'JavaScript'.search(/Script/) //4
'JavaScript'.search(/script/i) //4
'JavaScript'.search(/a+v/) //1
翻译自: https://flaviocopes.com/javascript-string-search/