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/