English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript String search() method

 JavaScript String Object

search()The method performs a search between the regular expression and the string match.

If a match is found, it will return the position of the first match. If no match is found, it will return-1.

You can find it inRegExp tutorialandRegExp object referenceLearn more about regular expressions.

Syntax:

string.search(regex)
var str = 'The question is to be, or not to be, that is to be.39
var pos = str.search('to be');
Test and see‹/›

Browser Compatibility

All browsers fully support the search() method:

Method
search()IsIsIsIsIs

Parameter Value

ParameterDescription
regexRegular Expression. If a string is passed, it is implicitly converted to a regular expression

Technical Details

Return value:The index of the first match between the regular expression and the given string; if not found, then-1
JavaScript Version:ECMAScript 1

More examples

The following examples demonstrateiUsage of Modifiers (case-insensitive):

var str = 'The question is TO BE, or not to be, that is to be.'
var pos = str.search(/to be/i);
Test and see‹/›

 JavaScript String Object