1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
---
title: String.prototype.match()
slug: Web/JavaScript/Reference/Global_Objects/String/match
tags:
- JavaScript
- Method
- Property
- Reference
- Regular Expression
- String
- match
- Übereinstimmung
translation_of: Web/JavaScript/Reference/Global_Objects/String/match
---
<div>{{JSRef}}</div>
<p><strong><code>match()</code></strong> gibt zurück, wo ein regulärer Ausdruck in einem String enthalten ist. </p>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox notranslate"><var>str</var>.match(<var>regexp</var>)</pre>
<h3 id="Parameter">Parameter</h3>
<dl>
<dt><code>regexp</code></dt>
<dd>Ein regulärer Ausdruck. Falls <code>regexp</code> kein Objekt vom Typ {{jsxref("RegExp")}} ist wird es mit Hilfe von <code>new RegExp(obj)</code> konvertiert. Falls dieser Parameter nicht übergeben wird, wird ein {{jsxref("Array")}} mit einem leeren {{jsxref("String")}} zurückgegben: [""].</dd>
</dl>
<h3 id="Rückgabewert">Rückgabewert</h3>
<p>Es wird ein {{jsxref("Array")}} zurückgegeben, das als erstes Element den gesamten übereinstimmenden String enthält, gefolgt von allen weiteren Treffern, die in Klammern angegeben wurden. Falls keine Übereinstimmungen gefunden wurden, wird {{jsxref("null")}} zurückgegeben.</p>
<h2 id="Beschreibung">Beschreibung</h2>
<p>Falls der reguläre Ausdruck nicht den Modifikator <code>g</code> enthält, verhält sich <strong><code>match()</code> </strong>wie {{jsxref("RegExp.prototype.exec()", "RegExp.exec()")}}. Im zurückgegebenen {{jsxref("Array")}} sind zusätzlich die propertys <code>input</code> und <code>index</code> enthalten. <code>input</code> ist gleich dem zu durchsuchenden String, <code>index</code> enthält den Index der Übereinstimmung.</p>
<p>Falls der <code>g</code> Modifikator im regulären Ausdruck enthalten ist, wird ein {{jsxref("Array")}} mit allen übereinstimmenden Substrings zurückgegeben.</p>
<h2 id="Beispiele">Beispiele</h2>
<h3 id="Verwendung_von_match">Verwendung von <code>match()</code></h3>
<p>Im folgenden Beispiel wird <strong><code>match()</code></strong> verwendet um <code>'Kapitel'</code> und anschließend eine oder mehrere Zahlen, gefolgt von einem Punkt und einer Zahl, 0 oder mehrmals zu finden. Der reguläre Ausdruck enthält den <code>i</code> Modifikator und unterscheidet somit nicht zwischen Groß- und Kleinschreibung.</p>
<pre class="brush: js notranslate">var str = 'Fuer mehr Informationen siehe Kapitel 3.4.5.1';
var re = /siehe (kapitel \d+(\.\d)*)/i;
var found = str.match(re);
console.log(found);
// gibt folgendes auf der Konsole aus:
// [ 'siehe Kapitel 3.4.5.1',
// 'Kapitel 3.4.5.1',
// '.1',
// index: 22,
// input: 'Fuer mehr Informationen siehe Kapitel 3.4.5.1' ]
</pre>
<h3 id="Verwendung_von_match_mit_den_i_und_g_Modifikatoren">Verwendung von <code>match()</code> mit den <code>i</code> und <code>g</code> Modifikatoren</h3>
<p>Die Buchstaben von A - E und von a - e werden auf der Konsole ausgegeben.</p>
<pre class="brush: js notranslate">var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var regexp = /[A-E]/gi;
var matches_array = str.match(regexp);
console.log(matches_array);
// ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
</pre>
<h3 id="match_ohne_Parameter"><code>match()</code> ohne Parameter</h3>
<pre class="brush: js notranslate">var str = "Nichts kommt von nichts.";
str.match();
// [""]</pre>
<h3 id="Verwendung_mit_einem_nicht_regulären_Ausdruck">Verwendung mit einem nicht regulären Ausdruck</h3>
<p>Falls ein String oder eine Zahl anstatt eines regulären Ausdrucks übergeben wird, wird der Parameter in einen {{jsxref("RegExp", "regulären Ausdruck")}} konvertiert. Ist es eine positive Zahl mit Vorzeichen, wird das positive Vorzeichen ignoriert.</p>
<pre class="brush: js notranslate">var str1 = "NaN bedeutet not a number. Infinity enthält -Infinity and +Infinity in JavaScript.",
str2 = "Zahlen: 1, 6, 3",
str3 = "null ist ein Datentyp.";
str1.match("number"); // ["number"]
str1.match(NaN); // ["NaN"]
str1.match(Infinity); // ["Infinity"]
str1.match(+Infinity); // ["Infinity"]
str1.match(-Infinity); // ["-Infinity"]
str2.match(6); // ["6"]
str2.match(+3); // ["3"]
str3.match(null); // ["null"]</pre>
<h2 id="Spezifikationen">Spezifikationen</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Spezifikation</th>
<th scope="col">Status</th>
<th scope="col">Extras</th>
</tr>
<tr>
<td>{{SpecName('ES3')}}</td>
<td>{{Spec2('ES3')}}</td>
<td>Anfangsdefinition. Implementiert in JavaScript 1.2.</td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-15.5.4.10', 'String.prototype.match')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-string.prototype.match', 'String.prototype.match')}}</td>
<td>{{Spec2('ES6')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-string.prototype.match', 'String.prototype.match')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="Browser-Kompatibilität">Browser-Kompatibilität</h2>
<p>{{Compat("javascript.builtins.String.match")}}</p>
<h2 id="Siehe_auch">Siehe auch</h2>
<ul>
<li>{{jsxref("RegExp")}}</li>
<li>{{jsxref("RegExp.prototype.exec()")}}</li>
<li>{{jsxref("RegExp.prototype.test()")}}</li>
</ul>
|