mozilla

Compare Revisions

String.prototype.match()

Change Revisions

Revision 71320:

Revision 71320 by Sheppy on

Revision 71321:

Revision 71321 by carpetsmoker 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 71320
Revision 71321
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">52    <h2 name="Description">
63          Description53      Description
64        </h2>54    </h2>
65        <p>55    <p>
66          If the regular expression does not include the <code>g<56      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>57    </p>
68        <p>58    <p>
69          If the regular expression includes the <code>g</code> f59      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>60    </p>
71        <p>61    <p>
72          The returned <code><a href="/en/JavaScript/Reference/Gl62      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.
63    </p>
64    <ul>
65      <li>
66        <ul>
67          <li>
68            <ul>
69              <li>
70                <dl>
71                  <dt>
72                    <code>regexp</code>
73                  </dt>
74                  <dd>
75                    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>.
76                  </dd>
77                </dl>
78              </li>
79            </ul>
80          </li>
81          <li>
82            <ul>
83              <li>
84                <dl>
85                  <dt>
86                    <code>regexp</code>
87                  </dt>
88                  <dd>
89                    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>.
90                  </dd>
91                </dl>
92              </li>
93            </ul>
94          </li>
73        </p>95        </ul>
74      </dd>96      </li>
97      <li>
98        <ul>
99          <li>
100            <ul>
101              <li>
102                <dl>
103                  <dt>
104                    <code>regexp</code>
105                  </dt>
106                  <dd>
107                    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>.
108                  </dd>
109                </dl>
110              </li>
111            </ul>
112          </li>
113          <li>
114            <ul>
115              <li>
116                <dl>
117                  <dt>
118                    <code>regexp</code>
119                  </dt>
120                  <dd>
121                    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>.
122                  </dd>
123                </dl>
124              </li>
125            </ul>
126          </li>
127        </ul>
128      </li>
75    </dl>129    </ul>
130    <ul>
131      <li>
132        <ul>
133          <li>
134            <ul>
135              <li>
136                <dl>
137                  <dt>
138                    <code>regexp</code>
139                  </dt>
140                  <dd>
141                    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>.
142                  </dd>
143                </dl>
144              </li>
145            </ul>
146          </li>
147          <li>
148            <ul>
149              <li>
150                <dl>
151                  <dt>
152                    <code>regexp</code>
153                  </dt>
154                  <dd>
155                    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>.
156                  </dd>
157                </dl>
158              </li>
159            </ul>
160          </li>
161        </ul>
162      </li>
163      <li>
164        <ul>
165          <li>
166            <ul>
167              <li>
168                <dl>
169                  <dt>
170                    <code>regexp</code>
171                  </dt>
172                  <dd>
173                    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>.
174                  </dd>
175                </dl>
176              </li>
177            </ul>
178          </li>
179          <li>
180            <ul>
181              <li>
182                <dl>
183                  <dt>
184                    <code>regexp</code>
185                  </dt>
186                  <dd>
187                    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>.
188                  </dd>
189                </dl>
190              </li>
191            </ul>
192          </li>
193        </ul>
194      </li>
195    </ul>
n84    <dl>n
85      <dd>
86        <h3 name="Notes">204    <h3 name="Notes">
87          Notes205      Notes
88        </h3>206    </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">207    <h2 name="Additional_Reading">
96          Additional Reading208      Additional Reading
97        </h2>209    </h2>
98        <ul>
99          <li>See §15.5.4.10 of the ECMA-262 specification.
100          </li>
101        </ul>
102        <h2 name="Examples">210    <h2 name="Examples">
103          Examples211      Examples
104        </h2>212    </h2>
105        <h3 name="Example:_Using_match">213    <h3 name="Example:_Using_match">
106          Example: Using <code>match</code>214      Example: Using <code>match</code>
107        </h3>215    </h3>
108        <p>216    <p>
109          In the following example, <code>match</code> is used to217      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>218    </p>
111        <pre>219    <pre>
n116document.write(found);n224alert(found);
n119        <p>n227    <p>
120          This returns the array containing Chapter 3.4.5.1,Chapt228      This returns the array containing Chapter 3.4.5.1,Chapter 3
>er 3.4.5.1,.1>.4.5.1,.1
121        </p>229    </p>
122        <p>230    <p>
123          "<code>Chapter 3.4.5.1</code>" is the first match and t231      "<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>232    </p>
125        <p>233    <p>
126          "<code>.1</code>" is the second value remembered from <234      "<code>.1</code>" is the second value remembered from <code
>code>(\.\d)</code>.>>(\.\d)</code>.
127        </p>235    </p>
128        <h3 name="Example:_Using_global_and_ignore_case_flags_wit236    <h3 name="Example:_Using_global_and_ignore_case_flags_with_ma
>h_match">>tch">
129          Example: Using global and ignore case flags with <code>237      Example: Using global and ignore case flags with <code>matc
>match</code>>h</code>
130        </h3>238    </h3>
131        <p>239    <p>
132          The following example demonstrates the use of the globa240      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>241    </p>
134        <pre>242    <pre>
n138document.write(matches_array);n246alert(matches_array);
t140        <p>t248    <p>
141          <code>matches_array</code> now equals <code>{{ mediawik249      <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>250    </p>
143        <p>251    <p>
144          &nbsp;252      &nbsp;
145        </p>253    </p>
146        <p>254    <p>
147          {{ languages( { "es": "es/Referencia_de_JavaScript_1.5/255      {{ 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" } ) }}
256    </p>
257    <ul>
258      <li>
259        <ul>
260          <li>
261            <ul>
262              <li>
263                <ul>
264                  <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>.
265                  </li>
266                  <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.
267                  </li>
268                </ul>
269                <ul>
270                  <li>See §15.5.4.10 of the ECMA-262 specificatio
 >n.
271                  </li>
272                </ul>
273              </li>
274            </ul>
275          </li>
276          <li>
277            <ul>
278              <li>
279                <ul>
280                  <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>.
281                  </li>
282                  <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.
283                  </li>
284                </ul>
285                <ul>
286                  <li>See §15.5.4.10 of the ECMA-262 specificatio
 >n.
287                  </li>
288                </ul>
289              </li>
290            </ul>
291          </li>
148        </p>292        </ul>
149      </dd>293      </li>
294      <li>
295        <ul>
296          <li>
297            <ul>
298              <li>
299                <ul>
300                  <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>.
301                  </li>
302                  <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.
303                  </li>
304                </ul>
305                <ul>
306                  <li>See §15.5.4.10 of the ECMA-262 specificatio
 >n.
307                  </li>
308                </ul>
309              </li>
310            </ul>
311          </li>
312          <li>
313            <ul>
314              <li>
315                <ul>
316                  <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>.
317                  </li>
318                  <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.
319                  </li>
320                </ul>
321                <ul>
322                  <li>See §15.5.4.10 of the ECMA-262 specificatio
 >n.
323                  </li>
324                </ul>
325              </li>
326            </ul>
327          </li>
328        </ul>
329      </li>
150    </dl>330    </ul>
331    <ul>
332      <li>
333        <ul>
334          <li>
335            <ul>
336              <li>
337                <ul>
338                  <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>.
339                  </li>
340                  <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.
341                  </li>
342                </ul>
343                <ul>
344                  <li>See §15.5.4.10 of the ECMA-262 specificatio
 >n.
345                  </li>
346                </ul>
347              </li>
348            </ul>
349          </li>
350          <li>
351            <ul>
352              <li>
353                <ul>
354                  <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>.
355                  </li>
356                  <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.
357                  </li>
358                </ul>
359                <ul>
360                  <li>See §15.5.4.10 of the ECMA-262 specificatio
 >n.
361                  </li>
362                </ul>
363              </li>
364            </ul>
365          </li>
366        </ul>
367      </li>
368      <li>
369        <ul>
370          <li>
371            <ul>
372              <li>
373                <ul>
374                  <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>.
375                  </li>
376                  <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.
377                  </li>
378                </ul>
379                <ul>
380                  <li>See §15.5.4.10 of the ECMA-262 specificatio
 >n.
381                  </li>
382                </ul>
383              </li>
384            </ul>
385          </li>
386          <li>
387            <ul>
388              <li>
389                <ul>
390                  <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>.
391                  </li>
392                  <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.
393                  </li>
394                </ul>
395                <ul>
396                  <li>See §15.5.4.10 of the ECMA-262 specificatio
 >n.
397                  </li>
398                </ul>
399              </li>
400            </ul>
401          </li>
402        </ul>
403      </li>
404    </ul>

Back to History