String search() method

Find out all the information about the JavaScript search() method of the string

Returns the position of the first occurrence of the string in the current string passed as a parameter.

It returns the index of the start of the event, or -1 if no event is found.

'JavaScript'.search('Script') //4
'JavaScript'.search('TypeScript') //-1

You can search using regular expressions (in fact, even if you pass a string, that string is used internally and transparently as a regular expression).

'JavaScript'.search(/Script/) //4
'JavaScript'.search(/script/i) //4
'JavaScript'.search(/a+v/) //1

Download mine for freeJavaScript beginner's manual

More js tutorials: