Revision 417423 of RegExp.prototype.test()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/RegExp/test
  • Revision title: test
  • Revision id: 417423
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment JavaScript/Reference/Global_Objects/RegExp/test Web/JavaScript/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
ECMAScript Edition ECMAScript 3rd Edition

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). As with exec (or in combination with it), test called multiple times on the same global regular expression instance will advance past the previous match.

{{ gecko_callout_heading("8.0") }}

Prior to Gecko 8.0 {{ geckoRelease("8.0") }}, test() was implemented incorrectly; when it was called with no parameters, it would match against the value of the previous input (RegExp.input property) instead of against the string "undefined". This is fixed; now /undefined/.test() correctly results in true, instead of an error.

Examples

Example: Using test

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

function testinput(re, str){
    var midstring;
    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>&nbsp;</p>
<h3 id="Summary" 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="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/RegExp" title="en/JavaScript/Reference/Global_Objects/RegExp"><code>RegExp</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.2</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 3rd Edition</td>
    </tr>
  </tbody>
</table>
<h3 id="Syntax" name="Syntax">Syntax</h3>
<p><code><em>regexp</em>.test({{ mediawiki.external('<i>str</i>') }}) </code></p>
<h3 id="Parameters" 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 id="Description" 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/JavaScript/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/JavaScript/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/JavaScript/Reference/Global_Objects/String/match" title="en/Core_JavaScript_1.5_Reference/Global_Objects/String/match">String.match</a></code> method). As with <code><code>exec</code></code> (or in combination with it), <code><code>test</code></code> called multiple times on the same global regular expression instance will advance past the previous match.</p>
<div class="geckoVersionNote">
  <p>{{ gecko_callout_heading("8.0") }}</p>
  <p>Prior to Gecko 8.0 {{ geckoRelease("8.0") }}, <code>test()</code> was implemented incorrectly; when it was called with no parameters, it would match against the value of the previous input (RegExp.input property) instead of against the string "undefined". This is fixed; now <code>/undefined/.test()</code> correctly results in <code>true</code>, instead of an error.</p>
</div>
<h3 id="Examples" name="Examples">Examples</h3>
<h4 id="Example:_Using_test" 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){
    var midstring;
    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