search()
String
instance method getterS2C Home
« Globals « String « search()
Description
Tries to locate a match between a regular expression and this String
object.
- If a match is located returns the index of the regular expression.
- If no match is located -1 is returned.
- When you wish to know if a pattern is found in a string use the
search()
method.
Similar to the regular expressionRegExp.test()
method. - You get more information at the cost of speed by using the
match()
method.
Similar to the regular expressionRegExp.exec()
method.
Syntax
Signature | Description |
---|---|
aString.search(regexp) | Tries to locate a match between a regular expression and this String object. |
Parameters
Parameter | Description |
---|---|
regexp | A regular expression or RegExp object.
|
Examples
The code below searches for numerics in a string and returns the index position of the first found.
// Create a string variable.
var aStringToSearch = 'The children had 12 toys';
/*
Search for numerics.
/*
alert(aStringToSearch.search(/\d+/));
Related Tutorials
JavaScript Basic Tutorials - Lesson 8 - Strings