RegExp.test() (Method)

Another name for the RegExp.exec() method when used with global RegExp objects.

Availability:

ECMAScript edition - 3
JavaScript - 1.2
JScript - 3.0
Internet Explorer - 4.0
Netscape - 4.0
Netscape Enterprise Server - 3.0
Opera - 5.0
Property/method value type:Boolean primitive
JavaScript syntax:-myRegExp.test(aString)
Argument list:aStringThe string to be examined by the regular expression.

This is not quite like the exec() method. In this case, you cannot establish whether the match occurs more than once or where in the string the match occurs.

This method simply returns true or false to indicate whether the pattern matches anything in the search string.

Example code:

   <HTML>

   <HEAD>

   </HEAD>

   <BODY>

   <SCRIPT>

   document.write("1 "     + testForYearNumber("1")     + "<BR>");

   document.write("12 "    + testForYearNumber("12")    + "<BR>");

   document.write("123 "   + testForYearNumber("123")   + "<BR>");

   document.write("1234 "  + testForYearNumber("1234")  + "<BR>");

   document.write("12345 " + testForYearNumber("12345") + "<BR>");

   function testForYearNumber(aString)

   {

      var myRegExp = /^\d{4}$/;

      return myRegExp.test(aString);

   }

   </SCRIPT>

   </BODY>

   </HTML>

See also:RegExp.$n, RegExp.exec(), RegExp.input, RegExp.lastMatch, RegExp.lastParen, RegExp.leftContext, RegExp.multiline, RegExp.rightContext

Cross-references:

ECMA 262 edition 3 - section - 15.10.6.3