RegExp.prototype.test()

  • Revision slug: JavaScript/Reference/Global_Objects/RegExp/test
  • Revision title: test
  • Revision id: 65243
  • Created:
  • Creator: Brettz9
  • Is current revision? No
  • Comment no wording changes

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('str') }})

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);
}

{{ languages( { "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/RegExp/test", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/RegExp/test" } ) }}

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/Global_Objects/RegExp" title="en/Core_JavaScript_1.5_Reference/Global_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> <em>regexp</em>.test({{ mediawiki.external('<i>str</i>') }}) </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/Global_Objects/String/search" title="en/Core_JavaScript_1.5_Reference/Global_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/Global_Objects/RegExp/exec" title="en/Core_JavaScript_1.5_Reference/Global_Objects/RegExp/exec">exec</a></code> method (similar to the <code><a href="/en/Core_JavaScript_1.5_Reference/Global_Objects/String/match" title="en/Core_JavaScript_1.5_Reference/Global_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 class="brush: js">function testinput(re, str){
   if (re.test(str))
      midstring = " contains ";
   else
      midstring = " does not contain ";
   document.write (str + midstring + re.source);
}
</pre>
<p>{{ languages( { "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/RegExp/test", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/RegExp/test" } ) }}</p>
Revert to this revision