contents   index   previous   next



RegExp test()

syntax:

regexp.test([str])

where:

str - a string on which to perform a regular expression match. Default is RegExp.input.

 

return:

boolean - true if there is a match, else false.

 

description:

Tests a string to see if there is a match for a regular expression pattern.

 

This method is equivalent to regexp.exec(string)!=null.

 

If there is a match, appropriate RegExp object static properties, such as RegExp.leftContext, RegExp.rightContext, RegExp.$n, and so forth, are set, providing more information about the matches.

 

Though it is unusual, test() may be used in a special way when the global attribute, "g", is set for a regular expression pattern. Like with RegExp exec(), when a match is found, the lastIndex property is set to the character position after the text match. Thus, test() may be used repeatedly on a string, though there are few reasons to do so. One reason would be if you only wanted to know if a string had more than one match.

 

see:

RegExp exec(), String match(), String search()

 

example:

var rtn;

var str = "one two three tio one";

var pat = /t.o/;

   // rtn == true

rtn = pat.test(str);


RegExp object static properties