String.search

search — Searches a string with a string or regular expression.

Description

String.search(mixed comparison, string with)

Parameters

Name Description Type Default Optional
comparison String or regexp mixed No
with String to replace with string No

Return values

number primitive.

Examples

Example #1 – search example

There a multiple ways to pass in the expression.

console.log('Hello World'.search(RegExp('l'))); // 2 console.log('Hello World'.search(new RegExp('l'))); // 2 console.log('Hello World'.search(/l/)); // 2 console.log('Hello World'.search('l')); // 2
Example #2 – search example

No match.

console.log('Hello World'.search(/a/)); // -1

External references