Description
The
search()
method matches a string against a regular expression **
The
search()
method returns the index (position) of the first match.
The
search()
method returns -1 if no match is found.
The
search()
method is case sensitive.
** If the search value is a string, it is converted to a regular expression.
See Also:
String Search
Regular Expression Tutorial
Regular Expression Reference
searchValue
Required.
The search value.
A regular expression (or a string that will be converted to a regular expression).
Return Value
Description
A numberThe position of the first match.
-1 if no match.
The Difference Between
String search() and String indexOf()
The
search()
cannot take a start position argument.
The
indexOf()
method cannot search against a regular expression.
The Difference Between
String search() and String match()
The
search()
method returns the position of the first match.
The
match()
method returns an array of matches.
Regular Expression Search Methods
In JavaScript, a regular expression text search, can be done with different methods.
With a
pattern
as a regular expression, these are the most common methods:
ExampleDescription
text.match(
pattern
)
The String method match()
text.search(
pattern
)
The String method search()
pattern
.exec(text)
The RexExp method exec()
pattern
.test(text)
The RegExp method test()
Contact Sales
If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]
Report Error
If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our
terms of use
,
cookie and privacy policy
.
W3Schools is Powered by W3.CSS
.