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
|
---
title: RegExp.prototype.test()
slug: Web/JavaScript/Reference/Global_Objects/RegExp/test
translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/test
---
<div>{{JSRef}}</div>
<p>Die <strong><code>test()</code></strong> Methode führt eine Suche nach einer Übereinstimmung zwischen einer regular expression und einem String durch. Sie gibt <code>true</code> oder <code>false</code> zurück.</p>
<div>{{EmbedInteractiveExample("pages/js/regexp-prototype-test.html", "taller")}}</div>
<p class="hidden">Der Quellcode dieses interaktiven Beispiels befindet sich in einer GitHub repository. Wenn Sie sich an diesem Projekt beteiligen möchten, clonen Sie bitte <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> und senden Sie uns einen pull request.</p>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox notranslate"><code><var>regexObj</var>.test(<var>str</var>)</code></pre>
<h3 id="Parameter">Parameter</h3>
<dl>
<dt><code><var>str</var></code></dt>
<dd>Der String, mit dem die regular expression verglichen wird.</dd>
</dl>
<h3 id="Rückgabewerte">Rückgabewerte</h3>
<p><code>true</code> wird zurückgegeben, wenn es eine Übereinstimmung zwischen der regular expression und dem String gibt <code><var>str</var></code>. Anderenfalls wird <code>false</code> zurückgegeben.</p>
<h2 id="Beschreibung">Beschreibung</h2>
<p>Verwenden Sie <code>test()</code>, wenn Sie wissen möchten, ob ein Muster auf einen String zutrifft. <code>test()</code> gibt einen boolschen Wert zurück, wohingegen die {{jsxref("String.prototype.search()")}} Methode den Index der Übereinstimmung, oder wenn keine Übereinstimmung gefunden wird, <code>-1</code> zurück gibt.</p>
<p>Um mehr Informationen zu erhalten, können Sie alternativ die langsammere {{jsxref("RegExp.prototype.exec()", "exec()")}} Methode verwenden. (Sie ähnelt {{jsxref("String.prototype.match()")}} Methode.)</p>
<p>As with <code>exec()</code> (or in combination with it), <code>test()</code> called multiple times on the same global regular expression instance will advance past the previous match.</p>
<h2 id="Beispiele">Beispiele</h2>
<h3 id="test_anwenden">test() anwenden</h3>
<p>Ein einfaches Beispiel, um zu testen, ob <code>"hello"</code> am Anfang eines Strings enthalten ist. Die Ausgabe ist ein boole'scher Wert. </p>
<pre class="brush: js notranslate">const str = 'hello world!';
const result = /^hello/.test(str);
console.log(result); // true
</pre>
<p>Das folgende Beispiel logt eine Nachricht, die vom Erfolg des Tests abhängt:</p>
<pre class="brush: js notranslate">function testInput(re, str) {
let midstring;
if (re.test(str)) {
midstring = 'contains';
} else {
midstring = 'does not contain';
}
console.log(`${str} ${midstring} ${re.source}`);
}
</pre>
<h3 id="Anwendung_von_test_mit_dem_Kennzeichen_global">Anwendung von test() mit dem Kennzeichen "global"</h3>
<p>When a regex has the <a href="/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Advanced_searching_with_flags_2">global flag</a> set, <code>test()</code> will advance the {{jsxref("RegExp.lastIndex", "lastIndex")}} of the regex. ({{jsxref("RegExp.prototype.exec()")}} also advances the <code>lastIndex</code> property.)</p>
<p>Further calls to <code>test(<var>str</var>)</code> will resume searching <code><var>str</var></code> starting from <code>lastIndex</code>. The <code>lastIndex</code> property will continue to increase each time <code>test()</code> returns <code>true</code>. </p>
<div class="blockIndicator note">
<p><strong>Note:</strong> As long as <code>test()</code> returns <code>true</code>, <code>lastIndex</code> will <em>not</em> reset—even when testing a different string!</p>
</div>
<p>When <code>test()</code> returns <code>false</code>, the calling regex's <code>lastIndex</code> property will reset to <code>0</code>.</p>
<p>The following example demonstrates this behaviour:</p>
<pre class="brush: js notranslate">const regex = /foo/g; // the "global" flag is set
// regex.lastIndex is at 0
regex.test('foo') // true
// regex.lastIndex is now at 3
regex.test('foo') // false
// regex.lastIndex is at 0
regex.test('barfoo') // true
// regex.lastIndex is at 6
regex.test('foobar') //false
// regex.lastIndex is at 0
// (...and so on)
</pre>
<h2 id="Spezifikationen">Spezifikationen</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Spezifikation</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('ESDraft', '#sec-regexp.prototype.test', 'RegExp.test')}}</td>
</tr>
</tbody>
</table>
<h2 id="Browser_-Kompatibilität">Browser -Kompatibilität</h2>
<p>{{Compat("javascript.builtins.RegExp.test")}}</p>
<h2 id="Firefox-specific_notes">Firefox-specific notes</h2>
<p>Prior to Firefox 8, <code>test()</code> was implemented incorrectly. When called without parameters, it would match against the value of the previous input (<code>RegExp.input</code> property) instead of against the string <code>"undefined"</code>.</p>
<p>This is fixed. Now, <code>/undefined/.test()</code> correctly results in <code>true</code>, instead of throwing an error.</p>
<h2 id="Siehe_auch">Siehe auch</h2>
<ul>
<li>Das Kapitel <a href="/en-US/docs/Web/JavaScript/Guide/Regular_Expressions">Regular Expressions</a> im <a href="/en-US/docs/Web/JavaScript/Guide">JavaScript Guide</a></li>
<li>{{jsxref("RegExp")}}</li>
<li>{{jsxref("RegExp.prototype")}}</li>
</ul>
|