mozilla

Revision 488263 of RegExp.prototype.test()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/RegExp/test
  • Revision title: RegExp.prototype.test()
  • Revision id: 488263
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment cleanup

Revision Content

{{JSRef("Global_Objects", "RegExp")}}

Summary

The test() method executes a search for a match between a regular expression and a specified string. Returns true or false.

Syntax

regexObj.test(str)

Parameters

str
The string against which to match the regular expression.

Return value

Boolean. true or false.

Description

You can use test() whenever want to know whether a pattern is found in a string (similar to the {{jsxref("String.search")}} method); for more information (but slower execution) use the{{jsxref("RegExp.exec", "exec")}} method (similar to the {{jsxref("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.

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 ";
    }
    console.log(str + midstring + re.source);
}

Specifications

Specification Status Comment
ECMAScript 3rd Edition. Implemented in JavaScript 1.2 Standard Initial definition.
{{SpecName('ES5.1', '#sec-15.10.6.3', 'RegExp.test')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-regexp.prototype.test', 'RegExp.test')}} {{Spec2('ES6')}}  

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}

Gecko-specific notes

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.

See also

Revision Source

<div>
 {{JSRef("Global_Objects", "RegExp")}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code><strong>test()</strong></code> method executes a search for a match between a regular expression and a specified string. Returns <code>true</code> or <code>false</code>.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre clas="syntaxbox">
<var>regexObj</var>.test(str)</pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
 <dt>
  <code>str</code></dt>
 <dd>
  The string against which to match the regular expression.</dd>
</dl>
<h3>Return value</h3>
<p>Boolean. <code>true</code> or <code>false</code>.</p>
<h2 id="Description" name="Description">Description</h2>
<p>You can use <code>test()</code> whenever want to know whether a pattern is found in a string (similar to the {{jsxref("String.search")}} method); for more information (but slower execution) use the{{jsxref("RegExp.exec", "exec")}} method (similar to the {{jsxref("String.match")}} 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>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Using_test" name="Example:_Using_test">Example: Using <code>test</code></h3>
<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 ";
    }
    console.log(str + midstring + re.source);
}
</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>ECMAScript 3rd Edition. Implemented in JavaScript 1.2</td>
   <td>Standard</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.10.6.3', 'RegExp.test')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-regexp.prototype.test', 'RegExp.test')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Chrome for Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<h3>Gecko-specific notes</h3>
<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>
<h2 id="See_also">See also</h2>
<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Guide/Regular_Expressions" title="JavaScript/Guide/Regular_Expressions">Regular Expressions</a> chapter in the <a href="/en-US/docs/Web/JavaScript/Guide" title="JavaScript/Guide">JavaScript Guide</a></li>
 <li>{{jsxref("Global_Objects/RegExp", "RegExp")}}</li>
</ul>
Revert to this revision