myString.search( searchExpr ) in ECMAScript 262

Find the offset of a regular expression within the string.

Arguments

nametypedescription
searchExpr RegExp The regular expression to sear
Return Type
Number

Description

Like String.indexOf(), this method returns the first index offset in the string where searchExpr occurs, or -1 if the regular expression cannot be found.

The global property of the regular expression, if set, is ignored. Only the first location is found, and unlike indexOf() there is no second parameter to start the search after a particular point. (Although this can be achieved equivalently via myString.slice(10).search(searchExpr).)

var sentence = "I'm in a hurry to get things done. I rush and rush until life's no fun.";
sentence += " All I really gotta do is live and die, but I'm in a hurry and don't know why.";
var wordsWithVowelBeforeN = /[a-z]*[aeiou]n[a-z']*/gi;
var allWords = sentence.match(wordsWithVowelBeforeN);
// ** allWords is ["in","things","done","and","until","fun","and","in","and","don't"]

var locationOfFirstMatch = sentence.search(wordsWithVowelBeforeN);
// ** locationOfFirstMatch is 4