RegExp.prototype.test()

  • Revision slug: JavaScript/Reference/Global_Objects/RegExp/test
  • Revision title: test
  • Revision id: 65240
  • Created:
  • Creator: Maian
  • Is current revision? No
  • Comment Core JavaScript 1.5 Reference:Objects:RegExp:test moved to Core JavaScript 1.5 Reference:Global Objects:RegExp:test

Revision Content

Summary

Executes the search for a match between a regular expression and a specified string. Returns true or false.

Method of RegExp
Implemented in: JavaScript 1.2, NES3.0
ECMA Version: ECMA-262, Edition 3

Syntax

regexp.test({{mediawiki.external('<i>str</i>')}})

Parameters

regexp 
The name of the regular expression. It can be a variable name or a literal.
str 
The string against which to match the regular expression.

Description

When you want to know whether a pattern is found in a string use the test method (similar to the String.search method); for more information (but slower execution) use the exec method (similar to the String.match method).

Examples

Example: Using test

The following example prints a message which depends on the success of the test:

function testinput(re, str){
   if (re.test(str))
      midstring = " contains ";
   else
      midstring = " does not contain ";
   document.write (str + midstring + re.source);
}

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p>Executes the search for a match between a regular expression and a specified string. Returns <code>true</code> or <code>false</code>.
</p>
<table class="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Method of <a href="en/Core_JavaScript_1.5_Reference/Objects/RegExp">RegExp</a></td>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.2, NES3.0</td>
</tr>
<tr>
<td>ECMA Version:</td>
<td>ECMA-262, Edition 3</td>
</tr>
</tbody></table>
<h3 name="Syntax"> Syntax </h3>
<p><code>
<i>regexp</i>.test({{mediawiki.external('&lt;i&gt;str&lt;/i&gt;')}})
</code>
</p>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>regexp</code> </dt><dd> The name of the regular expression. It can be a variable name or a literal.  
</dd></dl>
<dl><dt> <code>str</code> </dt><dd> The string against which to match the regular expression.
</dd></dl>
<h3 name="Description"> Description </h3>
<p>When you want to know whether a pattern is found in a string use the <code>test</code> method (similar to the <code><a href="en/Core_JavaScript_1.5_Reference/Objects/String/search">String.search</a></code> method); for more information (but slower execution) use the <code><a href="en/Core_JavaScript_1.5_Reference/Objects/RegExp/exec">exec</a></code> method (similar to the <code><a href="en/Core_JavaScript_1.5_Reference/Objects/String/match">String.match</a></code> method).
</p>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Using_test"> Example: Using <code>test</code> </h4>
<p>The following example prints a message which depends on the success of the test:
</p>
<pre>function testinput(re, str){
   if (re.test(str))
      midstring = " contains ";
   else
      midstring = " does not contain ";
   document.write (str + midstring + re.source);
}
</pre>
Revert to this revision