Compare Revisions

String.prototype.match()

Change Revisions

Revision 71315:

Revision 71315 by WenbingZheng on

Revision 71316:

Revision 71316 by WenbingZheng 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 71315
Revision 71316
t7    {{ localize('System.API.page-generated-for-subpage') }}t7    <h2 name="Summary">
8      摘要
9    </h2>
10    <p>
11      在使用正则表达式匹配时获取匹配结果。
12    </p>
13    <table class="standard-table">
14      <thead>
15        <tr>
16          <th class="header" colspan="2">
17            Method of <a href="/en/JavaScript/Reference/Global_Ob
 >jects/String" title="en/JavaScript/Reference/Global_Objects/Strin
 >g"><code>String</code></a>
18          </th>
19        </tr>
20      </thead>
21      <tbody>
22        <tr>
23          <td>
24            Implemented in
25          </td>
26          <td>
27            JavaScript 1.2
28          </td>
29        </tr>
30        <tr>
31          <td>
32            ECMAScript Edition
33          </td>
34          <td>
35            ECMAScript 3rd Edition
36          </td>
37        </tr>
38      </tbody>
39    </table>
40    <h2 name="Syntax">
41      语法
42    </h2>
43    <p>
44      <code><em>string</em>.match(<em>regexp</em>)</code>
45    </p>
46    <h2 name="Parameters">
47      参数
48    </h2>
49    <dl>
50      <dd>
51        <dl>
52          <dt>
53            <code>regexp</code>
54          </dt>
55          <dd>
56            一个<a href="/en/JavaScript/Reference/Global_Objects/Re
 >gExp" title="en/JavaScript/Reference/Global Objects/RegExp">正则表达式
 ></a>对象。如果传入一个非正则对象,会隐式地使用 <code>new RegExp(obj)</code> 转为一个正则表达式。
57          </dd>
58        </dl>
59        <h2 name="Description">
60          Description
61        </h2>
62        <p>
63          If the regular expression does not include the <code>g<
 >/code> flag, returns the same result as <code><a href="/en/JavaSc
 >ript/Reference/Global_Objects/RegExp/exec" title="en/Core_JavaScr
 >ipt_1.5_Reference/Global_Objects/RegExp/exec"><em>regexp</em>.exe
 >c(<em>string</em>)</a></code>.
64        </p>
65        <p>
66          If the regular expression includes the <code>g</code> f
 >lag, the method returns an <code><a href="/en/JavaScript/Referenc
 >e/Global_Objects/Array" title="en/Core_JavaScript_1.5_Reference/G
 >lobal_Objects/Array">Array</a></code> containing all matches. If 
 >there were no matches, the method returns <code>null</code>.
67        </p>
68        <p>
69          The returned <code><a href="/en/JavaScript/Reference/Gl
 >obal_Objects/Array" title="en/Core_JavaScript_1.5_Reference/Globa
 >l_Objects/Array">Array</a></code> has an extra <code>input</code>
 > property, which contains the regexp that generated it as a resul
 >t. In addition, it has an <code>index</code> property, which repr
 >esents the zero-based index of the match in the string.
70        </p>
71        <h3 name="Notes">
72          Notes
73        </h3>
74        <ul>
75          <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>.
76          </li>
77          <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.
78          </li>
79        </ul>
80        <h2 name="Additional_Reading">
81          Additional Reading
82        </h2>
83        <ul>
84          <li>See §15.5.4.10 of the ECMA-262 specification.
85          </li>
86        </ul>
87        <h2 name="Examples">
88          Examples
89        </h2>
90        <h3 name="Example:_Using_match">
91          Example: Using <code>match</code>
92        </h3>
93        <p>
94          In the following example, <code>match</code> is used to
 > find "<code>Chapter</code>" followed by 1 or more numeric charac
 >ters followed by a decimal point and numeric character 0 or more 
 >times. The regular expression includes the <code>i</code> flag so
 > that case will be ignored.
95        </p>
96        <pre>
97&lt;script type="text/javascript"&gt;
98str = "For more information, see Chapter 3.4.5.1";
99re = /(chapter \d+(\.\d)*)/i;
100found = str.match(re);
101document.write(found);
102&lt;/script&gt;
103</pre>
104        <p>
105          This returns the array containing Chapter 3.4.5.1,Chapt
 >er 3.4.5.1,.1
106        </p>
107        <p>
108          "<code>Chapter 3.4.5.1</code>" is the first match and t
 >he first value remembered from <code>(Chapter \d+(\.\d)*)</code>.
109        </p>
110        <p>
111          "<code>.1</code>" is the second value remembered from <
 >code>(\.\d)</code>.
112        </p>
113        <h3 name="Example:_Using_global_and_ignore_case_flags_wit
 >h_match">
114          Example: Using global and ignore case flags with <code>
 >match</code>
115        </h3>
116        <p>
117          The following example demonstrates the use of the globa
 >l and ignore case flags with <code>match</code>. All letters A th
 >rough E and a through e are returned, each its own element in the
 > array
118        </p>
119        <pre>
120var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
121var regexp = /[A-E]/gi;
122var matches_array = str.match(regexp);
123document.write(matches_array);
124</pre>
125        <p>
126          <code>matches_array</code> now equals <code>{{ mediawik
 >i.external('\'A\', \'B\', \'C\', \'D\', \'E\', \'a\', \'b\', \'c\
 >', \'d\', \'e\'') }}</code>
127        </p>
128        <p>
129          &nbsp;
130        </p>
131        <p>
132          {{ languages( { "es": "es/Referencia_de_JavaScript_1.5/
 >Objetos_globales/String/match", "ja": "ja/Core_JavaScript_1.5_Ref
 >erence/Global_Objects/String/match", "pl": "pl/Dokumentacja_j\u01
 >19zyka_JavaScript_1.5/Obiekty/String/match" } ) }}
133        </p>
134      </dd>
135    </dl>

Back to History