DEV Community

Discussion on: Javascript | String Methods

Collapse
 
lionelrowe profile image
lionel-rowe • Edited
// 2. search - true - search for the passed string and returns boolean
const stringSearch = str.search("World");

No, it searches for the passed regex and returns a number (the index of the match). If a string is passed, it's coerced to a regex before searching.

'abc.'.search('.') // 0
'$abc'.search('$') // 4
'[abc'.search('[') // throws Uncaught SyntaxError: Invalid regular expression
Enter fullscreen mode Exit fullscreen mode
Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

Yeah true, thanks for covering these cases