JS Built-ins
← String118 / 195
StringString.prototype.searchintermediate

String.prototype.search() β€” case-insensitive with /i

πŸ“– 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() β€” Case-Insensitive

Add the i flag to make the search case-insensitive.

Challenge: Implement findWordIndex(str, word) that returns the index of word (case-insensitive).

findWordIndex('Hello World', 'world') // β†’ 6
findWordIndex('Hello World', 'HELLO') // β†’ 0
πŸ’‘ Hints (1)
  • β€’ Use `new RegExp(word, "i")` to build a case-insensitive regex dynamically.