mozilla

Compare Revisions

RegExp.prototype.test()

Change Revisions

Revision 417423:

Revision 417423 by Sheppy on

Revision 488263:

Revision 488263 by fscholz on

Title:
test
RegExp.prototype.test()
Slug:
Web/JavaScript/Reference/Global_Objects/RegExp/test
Web/JavaScript/Reference/Global_Objects/RegExp/test
Tags:
"Regular Expressions"
"Regular Expressions", "JavaScript", "RegExp", "Method", "prototype"
Content:

Revision 417423
Revision 488263
n7    <p>n7    <div>
8      &nbsp;8      {{JSRef("Global_Objects", "RegExp")}}
9    </p>9    </div>
10    <h3 id="Summary" name="Summary">10    <h2 id="Summary" name="Summary">
n12    </h3>n12    </h2>
13    <p>
14      Executes the search for a match between a regular expressio
>n and a specified string. Returns <code>true</code> or <code>fals 
>e</code>. 
15    </p>13    <p>
16    <table class="standard-table">14      The <code><strong>test()</strong></code> method executes a 
 >search for a match between a regular expression and a specified s
 >tring. Returns <code>true</code> or <code>false</code>.
17      <thead>15    </p>
18        <tr>
19          <th class="header" colspan="2">
20            Method of <a href="/en/JavaScript/Reference/Global_Ob
>jects/RegExp" title="en/JavaScript/Reference/Global_Objects/RegEx 
>p"><code>RegExp</code></a> 
21          </th>
22        </tr>
23      </thead>
24      <tbody>
25        <tr>
26          <td>
27            Implemented in
28          </td>
29          <td>
30            JavaScript 1.2
31          </td>
32        </tr>
33        <tr>
34          <td>
35            ECMAScript Edition
36          </td>
37          <td>
38            ECMAScript 3rd Edition
39          </td>
40        </tr>
41      </tbody>
42    </table>
43    <h3 id="Syntax" name="Syntax">16    <h2 id="Syntax" name="Syntax">
n45    </h3>n18    </h2>
46    <p>19    <pre clas="syntaxbox">
47      <code><em>regexp</em>.test({{ mediawiki.external('<i>str</i20<var>regexObj</var>.test(str)
>>') }})</code> 
48    </p>21</pre>
n52    <dl>n
53      <dt>
54        <code>regexp</code>
55      </dt>
56      <dd>
57        The name of the regular expression. It can be a variable 
>name or a literal. 
58      </dd>
59    </dl>
nn33    <h3>
34      Return value
35    </h3>
36    <p>
37      Boolean. <code>true</code> or <code>false</code>.
38    </p>
68    <h3 id="Description" name="Description">39    <h2 id="Description" name="Description">
n70    </h3>n41    </h2>
71    <p>
72      When you want to know whether a pattern is found in a strin
>g 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">Stri 
>ng.search</a></code> method); for more information (but slower ex 
>ecution) use the <code><a href="/en/JavaScript/Reference/Global_O 
>bjects/RegExp/exec" title="en/Core_JavaScript_1.5_Reference/Globa 
>l_Objects/RegExp/exec">exec</a></code> method (similar to the <co 
>de><a href="/en/JavaScript/Reference/Global_Objects/String/match" 
> title="en/Core_JavaScript_1.5_Reference/Global_Objects/String/ma 
>tch">String.match</a></code> method). As with <code><code>exec</c 
>ode></code> (or in combination with it), <code><code>test</code>< 
>/code> called multiple times on the same global regular expressio 
>n instance will advance past the previous match. 
73    </p>42    <p>
74    <div class="geckoVersionNote">43      You can use <code>test()</code> whenever want to know wheth
 >er a pattern is found in a string (similar to the {{jsxref("Strin
 >g.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</cod
 >e></code> (or in combination with it), <code><code>test</code></c
 >ode> called multiple times on the same global regular expression 
 >instance will advance past the previous match.
75      <p>
76        {{ gecko_callout_heading("8.0") }}
77      </p>44    </p>
78      <p>
79        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 inpu 
>t (RegExp.input property) instead of against the string "undefine 
>d". This is fixed; now <code>/undefined/.test()</code> correctly  
>results in <code>true</code>, instead of an error. 
80      </p>
81    </div>
82    <h3 id="Examples" name="Examples">45    <h2 id="Examples" name="Examples">
n84    </h3>n47    </h2>
85    <h4 id="Example:_Using_test" name="Example:_Using_test">48    <h3 id="Example:_Using_test" name="Example:_Using_test">
n87    </h4>n50    </h3>
n99    document.write (str + midstring + re.source);n62    console.log(str + midstring + re.source);
tt65    <h2 id="Specifications">
66      Specifications
102    <p>67    </h2>
103      {{ languages( { "ja": "ja/Core_JavaScript_1.5_Reference/Glo68    <table class="standard-table">
>bal_Objects/RegExp/test", "pl": "pl/Dokumentacja_j\u0119zyka_Java 
>Script_1.5/Obiekty/RegExp/test" } ) }} 
69      <tbody>
70        <tr>
71          <th scope="col">
72            Specification
73          </th>
74          <th scope="col">
75            Status
76          </th>
77          <th scope="col">
78            Comment
79          </th>
80        </tr>
81        <tr>
82          <td>
83            ECMAScript 3rd Edition. Implemented in JavaScript 1.2
84          </td>
85          <td>
86            Standard
87          </td>
88          <td>
89            Initial definition.
90          </td>
91        </tr>
92        <tr>
93          <td>
94            {{SpecName('ES5.1', '#sec-15.10.6.3', 'RegExp.test')}
 >}
95          </td>
96          <td>
97            {{Spec2('ES5.1')}}
98          </td>
99          <td>
100            &nbsp;
101          </td>
102        </tr>
103        <tr>
104          <td>
105            {{SpecName('ES6', '#sec-regexp.prototype.test', 'RegE
 >xp.test')}}
106          </td>
107          <td>
108            {{Spec2('ES6')}}
109          </td>
110          <td>
111            &nbsp;
112          </td>
113        </tr>
114      </tbody>
115    </table>
116    <h2 id="Browser_compatibility">
117      Browser compatibility
118    </h2>
104    </p>119    <p>
120      {{ CompatibilityTable() }}
121    </p>
122    <div id="compat-desktop">
123      <table class="compat-table">
124        <tbody>
125          <tr>
126            <th>
127              Feature
128            </th>
129            <th>
130              Chrome
131            </th>
132            <th>
133              Firefox (Gecko)
134            </th>
135            <th>
136              Internet Explorer
137            </th>
138            <th>
139              Opera
140            </th>
141            <th>
142              Safari
143            </th>
144          </tr>
145          <tr>
146            <td>
147              Basic support
148            </td>
149            <td>
150              {{ CompatVersionUnknown() }}
151            </td>
152            <td>
153              {{ CompatVersionUnknown() }}
154            </td>
155            <td>
156              {{ CompatVersionUnknown() }}
157            </td>
158            <td>
159              {{ CompatVersionUnknown() }}
160            </td>
161            <td>
162              {{ CompatVersionUnknown() }}
163            </td>
164          </tr>
165        </tbody>
166      </table>
167    </div>
168    <div id="compat-mobile">
169      <table class="compat-table">
170        <tbody>
171          <tr>
172            <th>
173              Feature
174            </th>
175            <th>
176              Android
177            </th>
178            <th>
179              Chrome for Android
180            </th>
181            <th>
182              Firefox Mobile (Gecko)
183            </th>
184            <th>
185              IE Mobile
186            </th>
187            <th>
188              Opera Mobile
189            </th>
190            <th>
191              Safari Mobile
192            </th>
193          </tr>
194          <tr>
195            <td>
196              Basic support
197            </td>
198            <td>
199              {{ CompatVersionUnknown() }}
200            </td>
201            <td>
202              {{ CompatVersionUnknown() }}
203            </td>
204            <td>
205              {{ CompatVersionUnknown() }}
206            </td>
207            <td>
208              {{ CompatVersionUnknown() }}
209            </td>
210            <td>
211              {{ CompatVersionUnknown() }}
212            </td>
213            <td>
214              {{ CompatVersionUnknown() }}
215            </td>
216          </tr>
217        </tbody>
218      </table>
219    </div>
220    <h3>
221      Gecko-specific notes
222    </h3>
223    <p>
224      Prior to Gecko 8.0 {{ geckoRelease("8.0") }}, <code>test()<
 >/code> was implemented incorrectly; when it was called with no pa
 >rameters, 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 re
 >sults in <code>true</code>, instead of an error.
225    </p>
226    <h2 id="See_also">
227      See also
228    </h2>
229    <ul>
230      <li>
231        <a href="/en-US/docs/Web/JavaScript/Guide/Regular_Express
 >ions" title="JavaScript/Guide/Regular_Expressions">Regular Expres
 >sions</a> chapter in the <a href="/en-US/docs/Web/JavaScript/Guid
 >e" title="JavaScript/Guide">JavaScript Guide</a>
232      </li>
233      <li>{{jsxref("Global_Objects/RegExp", "RegExp")}}
234      </li>
235    </ul>

Back to History