mozilla

Compare Revisions

String.prototype.match()

Change Revisions

Revision 71322:

Revision 71322 by Sheppy on

Revision 71323:

Revision 71323 by Sheppy on

Title:
match
match
Slug:
JavaScript/Reference/Global_Objects/String/match
JavaScript/Reference/Global_Objects/String/match
Tags:
JavaScript, "JavaScript Reference", "Regular Expressions", "String Methods"
JavaScript, "JavaScript Reference", "Regular Expressions", "String Methods"
Content:

Revision 71322
Revision 71323
n12    </p>n
13    <p>
14      &nbsp;
n52    <dl>n
53      <dd>
54        <dl>
55          <dt>
56            <code>regexp</code>
57          </dt>
58          <dd>
59            A <a href="/en/JavaScript/Reference/Global_Objects/Re
>gExp" title="en/JavaScript/Reference/Global Objects/RegExp">regul 
>ar expression</a> object. If a non-RegExp object <code>obj</code> 
> is passed, it is implicitly converted to a RegExp by using <code 
>>new RegExp(obj)</code>. 
60          </dd>
61        </dl>
62        <h2 name="Description">49    <h2 name="Description">
63          Description50      Description
64        </h2>51    </h2>
65        <p>52    <p>
66          If the regular expression does not include the <code>g<53      If the regular expression does not include the <code>g</cod
>/code> flag, returns the same result as <code><a href="/en/JavaSc>e> flag, returns the same result as <code><a href="/en/JavaScript
>ript/Reference/Global_Objects/RegExp/exec" title="en/Core_JavaScr>/Reference/Global_Objects/RegExp/exec" title="en/Core_JavaScript_
>ipt_1.5_Reference/Global_Objects/RegExp/exec"><em>regexp</em>.exe>1.5_Reference/Global_Objects/RegExp/exec"><em>regexp</em>.exec(<e
>c(<em>string</em>)</a></code>.>m>string</em>)</a></code>.
67        </p>54    </p>
68        <p>55    <p>
69          If the regular expression includes the <code>g</code> f56      If the regular expression includes the <code>g</code> flag,
>lag, the method returns an <code><a href="/en/JavaScript/Referenc> the method returns an <code><a href="/en/JavaScript/Reference/Gl
>e/Global_Objects/Array" title="en/Core_JavaScript_1.5_Reference/G>obal_Objects/Array" title="en/Core_JavaScript_1.5_Reference/Globa
>lobal_Objects/Array">Array</a></code> containing all matches. If >l_Objects/Array">Array</a></code> containing all matches. If ther
>there were no matches, the method returns <code>null</code>.>e were no matches, the method returns <code>null</code>.
70        </p>57    </p>
71        <p>58    <p>
72          The returned <code><a href="/en/JavaScript/Reference/Gl59      The returned <code><a href="/en/JavaScript/Reference/Global
>obal_Objects/Array" title="en/Core_JavaScript_1.5_Reference/Globa>_Objects/Array" title="en/Core_JavaScript_1.5_Reference/Global_Ob
>l_Objects/Array">Array</a></code> has an extra <code>input</code>>jects/Array">Array</a></code> has an extra <code>input</code> pro
> property, which contains the regexp that generated it as a resul>perty, which contains the regexp that generated it as a result. I
>t. In addition, it has an <code>index</code> property, which repr>n addition, it has an <code>index</code> property, which represen
>esents the zero-based index of the match in the string.>ts the zero-based index of the match in the string.
60    </p>
61    <ul>
62      <li>
63        <ul>
64          <li>
65            <ul>
66              <li>
67                <dl>
68                  <dt>
69                    <code>regexp</code>
70                  </dt>
71                  <dd>
72                    A <a href="/en/JavaScript/Reference/Global_Ob
 >jects/RegExp" title="en/JavaScript/Reference/Global Objects/RegEx
 >p">regular expression</a> object. If a non-RegExp object <code>ob
 >j</code> is passed, it is implicitly converted to a RegExp by usi
 >ng <code>new RegExp(obj)</code>.
73                  </dd>
74                </dl>
75              </li>
76            </ul>
77          </li>
78          <li>
79            <ul>
80              <li>
81                <dl>
82                  <dt>
83                    <code>regexp</code>
84                  </dt>
85                  <dd>
86                    A <a href="/en/JavaScript/Reference/Global_Ob
 >jects/RegExp" title="en/JavaScript/Reference/Global Objects/RegEx
 >p">regular expression</a> object. If a non-RegExp object <code>ob
 >j</code> is passed, it is implicitly converted to a RegExp by usi
 >ng <code>new RegExp(obj)</code>.
87                  </dd>
88                </dl>
89              </li>
90            </ul>
91          </li>
73        </p>92        </ul>
74      </dd>93      </li>
94      <li>
95        <ul>
96          <li>
97            <ul>
98              <li>
99                <dl>
100                  <dt>
101                    <code>regexp</code>
102                  </dt>
103                  <dd>
104                    A <a href="/en/JavaScript/Reference/Global_Ob
 >jects/RegExp" title="en/JavaScript/Reference/Global Objects/RegEx
 >p">regular expression</a> object. If a non-RegExp object <code>ob
 >j</code> is passed, it is implicitly converted to a RegExp by usi
 >ng <code>new RegExp(obj)</code>.
105                  </dd>
106                </dl>
107              </li>
108            </ul>
109          </li>
110          <li>
111            <ul>
112              <li>
113                <dl>
114                  <dt>
115                    <code>regexp</code>
116                  </dt>
117                  <dd>
118                    A <a href="/en/JavaScript/Reference/Global_Ob
 >jects/RegExp" title="en/JavaScript/Reference/Global Objects/RegEx
 >p">regular expression</a> object. If a non-RegExp object <code>ob
 >j</code> is passed, it is implicitly converted to a RegExp by usi
 >ng <code>new RegExp(obj)</code>.
119                  </dd>
120                </dl>
121              </li>
122            </ul>
123          </li>
124        </ul>
125      </li>
75    </dl>126    </ul>
127    <ul>
128      <li>
129        <ul>
130          <li>
131            <ul>
132              <li>
133                <dl>
134                  <dt>
135                    <code>regexp</code>
136                  </dt>
137                  <dd>
138                    A <a href="/en/JavaScript/Reference/Global_Ob
 >jects/RegExp" title="en/JavaScript/Reference/Global Objects/RegEx
 >p">regular expression</a> object. If a non-RegExp object <code>ob
 >j</code> is passed, it is implicitly converted to a RegExp by usi
 >ng <code>new RegExp(obj)</code>.
139                  </dd>
140                </dl>
141              </li>
142            </ul>
143          </li>
144          <li>
145            <ul>
146              <li>
147                <dl>
148                  <dt>
149                    <code>regexp</code>
150                  </dt>
151                  <dd>
152                    A <a href="/en/JavaScript/Reference/Global_Ob
 >jects/RegExp" title="en/JavaScript/Reference/Global Objects/RegEx
 >p">regular expression</a> object. If a non-RegExp object <code>ob
 >j</code> is passed, it is implicitly converted to a RegExp by usi
 >ng <code>new RegExp(obj)</code>.
153                  </dd>
154                </dl>
155              </li>
156            </ul>
157          </li>
158        </ul>
159      </li>
160      <li>
161        <ul>
162          <li>
163            <ul>
164              <li>
165                <dl>
166                  <dt>
167                    <code>regexp</code>
168                  </dt>
169                  <dd>
170                    A <a href="/en/JavaScript/Reference/Global_Ob
 >jects/RegExp" title="en/JavaScript/Reference/Global Objects/RegEx
 >p">regular expression</a> object. If a non-RegExp object <code>ob
 >j</code> is passed, it is implicitly converted to a RegExp by usi
 >ng <code>new RegExp(obj)</code>.
171                  </dd>
172                </dl>
173              </li>
174            </ul>
175          </li>
176          <li>
177            <ul>
178              <li>
179                <dl>
180                  <dt>
181                    <code>regexp</code>
182                  </dt>
183                  <dd>
184                    A <a href="/en/JavaScript/Reference/Global_Ob
 >jects/RegExp" title="en/JavaScript/Reference/Global Objects/RegEx
 >p">regular expression</a> object. If a non-RegExp object <code>ob
 >j</code> is passed, it is implicitly converted to a RegExp by usi
 >ng <code>new RegExp(obj)</code>.
185                  </dd>
186                </dl>
187              </li>
188            </ul>
189          </li>
190        </ul>
191      </li>
192    </ul>
n84    <dl>n
85      <dd>
86        <h3 name="Notes">201    <h3 name="Notes">
87          Notes202      Notes
88        </h3>203    </h3>
89        <ul>
90          <li>If you need to know if a string matches a regular e
>xpression <code>regexp</code>, use <code><a href="/en/JavaScript/ 
>Reference/Global_Objects/RegExp/test" title="en/Core_JavaScript_1 
>.5_Reference/Global_Objects/RegExp/test"><em>regexp</em>.test(<em 
>>string</em>)</a></code>. 
91          </li>
92          <li>If you only want the first match found, you might w
>ant to use <code><a href="/en/JavaScript/Reference/Global_Objects 
>/RegExp/exec" title="en/Core_JavaScript_1.5_Reference/Global_Obje 
>cts/RegExp/exec"><em>regexp</em>.exec(<em>string</em>)</a></code> 
> instead. 
93          </li>
94        </ul>
95        <h2 name="Additional_Reading">204    <h2 name="Additional_Reading">
96          Additional Reading205      Additional Reading
97        </h2>206    </h2>
98        <ul>
99          <li>See §15.5.4.10 of the ECMA-262 specification.
100          </li>
101        </ul>
102        <h2 name="Examples">207    <h2 name="Examples">
103          Examples208      Examples
104        </h2>209    </h2>
105        <h3 name="Example:_Using_match">210    <h3 name="Example:_Using_match">
106          Example: Using <code>match</code>211      Example: Using <code>match</code>
107        </h3>212    </h3>
108        <p>213    <p>
109          In the following example, <code>match</code> is used to214      In the following example, <code>match</code> is used to fin
> find "<code>Chapter</code>" followed by 1 or more numeric charac>d "<code>Chapter</code>" followed by 1 or more numeric characters
>ters followed by a decimal point and numeric character 0 or more > followed by a decimal point and numeric character 0 or more time
>times. The regular expression includes the <code>i</code> flag so>s. The regular expression includes the <code>i</code> flag so tha
> that case will be ignored.>t case will be ignored.
110        </p>215    </p>
111        <pre>216    <pre class="brush: html">
n119        <p>n224    <p>
120          This returns the array containing Chapter 3.4.5.1,Chapt225      This returns the array containing Chapter 3.4.5.1,Chapter 3
>er 3.4.5.1,.1>.4.5.1,.1
121        </p>226    </p>
122        <p>227    <p>
123          "<code>Chapter 3.4.5.1</code>" is the first match and t228      "<code>Chapter 3.4.5.1</code>" is the first match and the f
>he first value remembered from <code>(Chapter \d+(\.\d)*)</code>.>irst value remembered from <code>(Chapter \d+(\.\d)*)</code>.
124        </p>229    </p>
125        <p>230    <p>
126          "<code>.1</code>" is the second value remembered from <231      "<code>.1</code>" is the second value remembered from <code
>code>(\.\d)</code>.>>(\.\d)</code>.
127        </p>232    </p>
128        <h3 name="Example:_Using_global_and_ignore_case_flags_wit233    <h3 name="Example:_Using_global_and_ignore_case_flags_with_ma
>h_match">>tch">
129          Example: Using global and ignore case flags with <code>234      Example: Using global and ignore case flags with <code>matc
>match</code>>h</code>
130        </h3>235    </h3>
131        <p>236    <p>
132          The following example demonstrates the use of the globa237      The following example demonstrates the use of the global an
>l and ignore case flags with <code>match</code>. All letters A th>d ignore case flags with <code>match</code>. All letters A throug
>rough E and a through e are returned, each its own element in the>h E and a through e are returned, each its own element in the arr
> array>ay
133        </p>238    </p>
134        <pre>239    <pre class="brush: html">
t140        <p>t245    <p>
141          <code>matches_array</code> now equals <code>{{ mediawik246      <code>matches_array</code> now equals <code>{{ mediawiki.ex
>i.external('\'A\', \'B\', \'C\', \'D\', \'E\', \'a\', \'b\', \'c\>ternal('\'A\', \'B\', \'C\', \'D\', \'E\', \'a\', \'b\', \'c\', \
>', \'d\', \'e\'') }}</code>>'d\', \'e\'') }}</code>
142        </p>247    </p>
143        <p>248    <p>
144          &nbsp;
145        </p>
146        <p>
147          {{ languages( { "es": "es/Referencia_de_JavaScript_1.5/249      {{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Obje
>Objetos_globales/String/match", "ja": "ja/Core_JavaScript_1.5_Ref>tos_globales/String/match", "ja": "ja/Core_JavaScript_1.5_Referen
>erence/Global_Objects/String/match", "pl": "pl/Dokumentacja_j\u01>ce/Global_Objects/String/match", "pl": "pl/Dokumentacja_j\u0119zy
>19zyka_JavaScript_1.5/Obiekty/String/match" } ) }}>ka_JavaScript_1.5/Obiekty/String/match" } ) }}
250    </p>
251    <ul>
252      <li>
253        <ul>
254          <li>
255            <ul>
256              <li>
257                <ul>
258                  <li>If you need to know if a string matches a r
 >egular expression <code>regexp</code>, use <code><a href="/en/Jav
 >aScript/Reference/Global_Objects/RegExp/test" title="en/Core_Java
 >Script_1.5_Reference/Global_Objects/RegExp/test"><em>regexp</em>.
 >test(<em>string</em>)</a></code>.
259                  </li>
260                  <li>If you only want the first match found, you
 > might want to use <code><a href="/en/JavaScript/Reference/Global
 >_Objects/RegExp/exec" title="en/Core_JavaScript_1.5_Reference/Glo
 >bal_Objects/RegExp/exec"><em>regexp</em>.exec(<em>string</em>)</a
 >></code> instead.
261                  </li>
262                </ul>
263                <ul>
264                  <li>See §15.5.4.10 of the ECMA-262 specificatio
 >n.
265                  </li>
266                </ul>
267              </li>
268            </ul>
269          </li>
270          <li>
271            <ul>
272              <li>
273                <ul>
274                  <li>If you need to know if a string matches a r
 >egular expression <code>regexp</code>, use <code><a href="/en/Jav
 >aScript/Reference/Global_Objects/RegExp/test" title="en/Core_Java
 >Script_1.5_Reference/Global_Objects/RegExp/test"><em>regexp</em>.
 >test(<em>string</em>)</a></code>.
275                  </li>
276                  <li>If you only want the first match found, you
 > might want to use <code><a href="/en/JavaScript/Reference/Global
 >_Objects/RegExp/exec" title="en/Core_JavaScript_1.5_Reference/Glo
 >bal_Objects/RegExp/exec"><em>regexp</em>.exec(<em>string</em>)</a
 >></code> instead.
277                  </li>
278                </ul>
279                <ul>
280                  <li>See §15.5.4.10 of the ECMA-262 specificatio
 >n.
281                  </li>
282                </ul>
283              </li>
284            </ul>
285          </li>
148        </p>286        </ul>
149      </dd>287      </li>
288      <li>
289        <ul>
290          <li>
291            <ul>
292              <li>
293                <ul>
294                  <li>If you need to know if a string matches a r
 >egular expression <code>regexp</code>, use <code><a href="/en/Jav
 >aScript/Reference/Global_Objects/RegExp/test" title="en/Core_Java
 >Script_1.5_Reference/Global_Objects/RegExp/test"><em>regexp</em>.
 >test(<em>string</em>)</a></code>.
295                  </li>
296                  <li>If you only want the first match found, you
 > might want to use <code><a href="/en/JavaScript/Reference/Global
 >_Objects/RegExp/exec" title="en/Core_JavaScript_1.5_Reference/Glo
 >bal_Objects/RegExp/exec"><em>regexp</em>.exec(<em>string</em>)</a
 >></code> instead.
297                  </li>
298                </ul>
299                <ul>
300                  <li>See §15.5.4.10 of the ECMA-262 specificatio
 >n.
301                  </li>
302                </ul>
303              </li>
304            </ul>
305          </li>
306          <li>
307            <ul>
308              <li>
309                <ul>
310                  <li>If you need to know if a string matches a r
 >egular expression <code>regexp</code>, use <code><a href="/en/Jav
 >aScript/Reference/Global_Objects/RegExp/test" title="en/Core_Java
 >Script_1.5_Reference/Global_Objects/RegExp/test"><em>regexp</em>.
 >test(<em>string</em>)</a></code>.
311                  </li>
312                  <li>If you only want the first match found, you
 > might want to use <code><a href="/en/JavaScript/Reference/Global
 >_Objects/RegExp/exec" title="en/Core_JavaScript_1.5_Reference/Glo
 >bal_Objects/RegExp/exec"><em>regexp</em>.exec(<em>string</em>)</a
 >></code> instead.
313                  </li>
314                </ul>
315                <ul>
316                  <li>See §15.5.4.10 of the ECMA-262 specificatio
 >n.
317                  </li>
318                </ul>
319              </li>
320            </ul>
321          </li>
322        </ul>
323      </li>
150    </dl>324    </ul>
325    <ul>
326      <li>
327        <ul>
328          <li>
329            <ul>
330              <li>
331                <ul>
332                  <li>If you need to know if a string matches a r
 >egular expression <code>regexp</code>, use <code><a href="/en/Jav
 >aScript/Reference/Global_Objects/RegExp/test" title="en/Core_Java
 >Script_1.5_Reference/Global_Objects/RegExp/test"><em>regexp</em>.
 >test(<em>string</em>)</a></code>.
333                  </li>
334                  <li>If you only want the first match found, you
 > might want to use <code><a href="/en/JavaScript/Reference/Global
 >_Objects/RegExp/exec" title="en/Core_JavaScript_1.5_Reference/Glo
 >bal_Objects/RegExp/exec"><em>regexp</em>.exec(<em>string</em>)</a
 >></code> instead.
335                  </li>
336                </ul>
337                <ul>
338                  <li>See §15.5.4.10 of the ECMA-262 specificatio
 >n.
339                  </li>
340                </ul>
341              </li>
342            </ul>
343          </li>
344          <li>
345            <ul>
346              <li>
347                <ul>
348                  <li>If you need to know if a string matches a r
 >egular expression <code>regexp</code>, use <code><a href="/en/Jav
 >aScript/Reference/Global_Objects/RegExp/test" title="en/Core_Java
 >Script_1.5_Reference/Global_Objects/RegExp/test"><em>regexp</em>.
 >test(<em>string</em>)</a></code>.
349                  </li>
350                  <li>If you only want the first match found, you
 > might want to use <code><a href="/en/JavaScript/Reference/Global
 >_Objects/RegExp/exec" title="en/Core_JavaScript_1.5_Reference/Glo
 >bal_Objects/RegExp/exec"><em>regexp</em>.exec(<em>string</em>)</a
 >></code> instead.
351                  </li>
352                </ul>
353                <ul>
354                  <li>See §15.5.4.10 of the ECMA-262 specificatio
 >n.
355                  </li>
356                </ul>
357              </li>
358            </ul>
359          </li>
360        </ul>
361      </li>
362      <li>
363        <ul>
364          <li>
365            <ul>
366              <li>
367                <ul>
368                  <li>If you need to know if a string matches a r
 >egular expression <code>regexp</code>, use <code><a href="/en/Jav
 >aScript/Reference/Global_Objects/RegExp/test" title="en/Core_Java
 >Script_1.5_Reference/Global_Objects/RegExp/test"><em>regexp</em>.
 >test(<em>string</em>)</a></code>.
369                  </li>
370                  <li>If you only want the first match found, you
 > might want to use <code><a href="/en/JavaScript/Reference/Global
 >_Objects/RegExp/exec" title="en/Core_JavaScript_1.5_Reference/Glo
 >bal_Objects/RegExp/exec"><em>regexp</em>.exec(<em>string</em>)</a
 >></code> instead.
371                  </li>
372                </ul>
373                <ul>
374                  <li>See §15.5.4.10 of the ECMA-262 specificatio
 >n.
375                  </li>
376                </ul>
377              </li>
378            </ul>
379          </li>
380          <li>
381            <ul>
382              <li>
383                <ul>
384                  <li>If you need to know if a string matches a r
 >egular expression <code>regexp</code>, use <code><a href="/en/Jav
 >aScript/Reference/Global_Objects/RegExp/test" title="en/Core_Java
 >Script_1.5_Reference/Global_Objects/RegExp/test"><em>regexp</em>.
 >test(<em>string</em>)</a></code>.
385                  </li>
386                  <li>If you only want the first match found, you
 > might want to use <code><a href="/en/JavaScript/Reference/Global
 >_Objects/RegExp/exec" title="en/Core_JavaScript_1.5_Reference/Glo
 >bal_Objects/RegExp/exec"><em>regexp</em>.exec(<em>string</em>)</a
 >></code> instead.
387                  </li>
388                </ul>
389                <ul>
390                  <li>See §15.5.4.10 of the ECMA-262 specificatio
 >n.
391                  </li>
392                </ul>
393              </li>
394            </ul>
395          </li>
396        </ul>
397      </li>
398    </ul>

Back to History