test()
RegExp
instance method getterS2C Home
« Globals « RegExp « test()
Description
Returns true
or false
for a match between a regular expression and a string.
- Will advance past the previous match when called multiple times on the same regular expression.
Syntax
Signature | Description |
---|---|
result = regexp.test(string) | Returns true or false for a match between a regular expression and a string |
Parameters
Signature | Description |
---|---|
regexp | The regular expression.
|
string | The string to match the regular expression with. |
Examples
The code below displays the test()
method on a string.
var aRegExp = /and/g;
var aString = new String 'one and two and three and four';
var testValues = new Array();
var i = 0;
while (aRegExp.test(aString)==true) {
testValues[i] = 'test() found a result.' + aRegExp.test(aString)
+ ' Match will continue at index: '
+ aRegExp.lastIndex + '\n';
i++;
}
alert(testValues);
Related Tutorials
JavaScript Intermediate Tutorials - Lesson 9 - Regular Expressions