RegExp.prototype.test()

  • Revision slug: JavaScript/Reference/Global_Objects/RegExp/test
  • Revision title: test
  • Revision id: 65250
  • Created:
  • Creator: evilpie
  • Is current revision? No
  • Comment 60 words added

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.

{{ Fx_minversion_note("8") }}

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="standard-table"> <thead> <tr> <th class="header" colspan="2">Method of <a href="/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 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/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> <p>{{ Fx_minversion_note("8") }}</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