JS Built-ins
← String117 / 195
StringString.prototype.searchbeginner

String.prototype.search() β€” -1 when not found

πŸ“– Usage Example
'hello world'.search(/world/)   // β†’ 6
'hello'.search(/d/)           // β†’ -1
'abc123'.search(/[0-9]/)       // β†’ 3

Use search() to find the index of the first regex match in a string, returning -1 if not found.

String.prototype.search() β€” Not Found

When the regex has no match, .search() returns -1.

Challenge: Implement hasUpperCase(str) that returns true if the string contains an uppercase letter.

hasUpperCase('Hello') // β†’ true
hasUpperCase('hello') // β†’ false
πŸ’‘ Hints (1)
  • β€’ `str.search(/[A-Z]/)` returns `-1` if no uppercase letter is found.