Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

String.prototype.search

Returns the position of the first regex match.

Available since: ES3

Syntax

str.search(regexp)

Parameters

ParameterTypeDescription
regexpRegExpA regular expression.

Return value

numberThe position of the first match, or -1.

Examples

Where the first digit is:

console.log('Room A12'.search(/\d/));
console.log('Room A'.search(/\d/));

Output:

6
-1

Notes

It is indexOf() for regular expressions. If you only need to know whether there is a match, regex.test(str) is simpler.

See also

Related recipes

In the tutorial

  • Regular expressions — Regular expressions in JavaScript for beginners: patterns, flags, character classes, testing, finding and replacing text.

Thanks for reading!

Donate to the site — we work hard for you.