JS: String.prototype.search

By Xah Lee. Date: . Last updated: .
str.search(regex_or_str)
  • Return the index of first char of matched string.
  • If no match, returns -1.
  • If argument is string, it is converted to regex by RegExp
// find digit
console.log("a 99 b".search(/\d/) === 2);
// true

// using string as arg
console.log("a 99 b".search("\\d") === 2);
// true

// not found
console.log("a 99 b".search("ttt") === -1);
// true

JavaScript. Regular Expression

JS String.prototype