Die Methode indexOf()
gibt den Index (Position) zurück. das erste Vorkommen einer Zeichenfolge in einer Zeichenfolge:
let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("locate");
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The indexOf() Method</h2>
<p>The indexOf() method returns the position of the first occurrence of a string in a string.</p>
<p>The position of the first occurrence of "locate" is:</p>
<p id="demo"></p>
<script>
let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("locate");
document.getElementById("demo").innerHTML = index;
</script>
</body>
</html>
JavaScript zählt Positionen von Null an.
0 ist die erste Position in a Zeichenfolge, 1 ist die zweite, 2 ist die dritte, ...
Die Methode lastIndexOf()
gibt den Index des letzten zurück Vorkommen eines angegebenen Textes in einer Zeichenfolge:
let text = "Please locate where 'locate' occurs!";
let index = text.lastIndexOf("locate");
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The lastIndexOf() Method</h2>
<p>The position of the last occurrence of "locate" is:</p>
<p id="demo"></p>
<script>
let text = "Please locate where 'locate' occurs!";
let index = text.lastIndexOf("locate");
document.getElementById("demo").innerHTML = index;
</script>
</body>
</html>
Sowohl indexOf()
als auch lastIndexOf()
geben -1 zurück wenn der Text nicht gefunden wird:
let text = "Please locate where 'locate' occurs!";
let index = text.lastIndexOf("John");
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The indexOf() Method</h2>
<p>Both indexOf() and lastIndexOf() return -1 if the text is not found:</p>
<p id="demo"></p>
<script>
let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("John");
document.getElementById("demo").innerHTML = index;
</script>
</body>
</html>
Beide Methoden akzeptieren einen zweiten Parameter als Startposition für suchen:
let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("locate", 15);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The indexOf() Method</h2>
<p>The indexOf() method accepts a second parameter as the starting position for the search:</p>
<p id="demo"></p>
<script>
let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("locate",15);
document.getElementById("demo").innerHTML = index;
</script>
</body>
</html>
Die lastIndexOf()
-Methode sucht rückwärts (vom Ende zum Anfang), was bedeutet: Wenn der zweite Parameter 15
ist, beginnt die Suche an der Position 15 und sucht bis zum Anfang der Zeichenfolge.
let text = "Please locate where 'locate' occurs!";
text.lastIndexOf("locate", 15);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The lastIndexOf() Method</h2>
<p>The lastIndexOf() method accepts a second parameter as the starting position for the search.</p>
<p>Remember that the lastIndexOf() method searches backwards, so position 15 means start the search at position 15, and search to the beginning.</p>
<p id="demo"></p>
<script>
let text = "Please locate where 'locate' occurs!";
let index = text.lastIndexOf("locate", 15);
document.getElementById("demo").innerHTML = index;
</script>
</body>
</html>
Die Methode search()
durchsucht eine Zeichenfolge nach einer Zeichenfolge (oder einem regulären Ausdruck). und gibt die Position der Übereinstimmung zurück:
let text = "Please locate where 'locate' occurs!";
text.search("locate");
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The search() Method</h2>
<p>The search() method returns the position of the first occurrence of a string in a string.</p>
<p>The position of the first occurrence of "locate" is:</p>
<p id="demo"></p>
<script>
let text = "Please locate where 'locate' occurs!";
let index = text.search("locate");
document.getElementById("demo").innerHTML = index;
</script>
</body>
</html>
let text = "Please locate where 'locate' occurs!";
text.search(/locate/);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The search() Method</h2>
<p>The search() method returns the position of the first occurrence of a string in a string.</p>
<p>Return the position of the first occurrence of a regular expression:</p>
<p id="demo"></p>
<script>
let text = "Please locate where 'locate' occurs!";
let index = text.search(/locate/);
document.getElementById("demo").innerHTML = index;
</script>
</body>
</html>
Die beiden Methoden indexOf()
und search()
sind gleich?
Sie akzeptieren dieselben Argumente (Parameter) und geben denselben Wert zurück?
Die beiden Methoden sind NICHT gleich. Das sind die Unterschiede:
Die Methode search()
kann kein zweites Startpositionsargument annehmen.
Die Methode indexOf()
kann nicht verwendet werden leistungsstarke Suchwerte (reguläre Ausdrücke).
Sie erfahren mehr darüber Reguläre Ausdrücke finden Sie in einem späteren Kapitel.
Die Methode match()
gibt ein Array zurück, das die Ergebnisse des Abgleichs enthält eine Zeichenfolge gegen eine Zeichenfolge (oder einen regulären Ausdruck).
Führen Sie eine Suche nach „ain“ durch:
let text = "The rain in SPAIN stays mainly in the plain";
text.match("ain");
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The match() Method</h2>
<p>Perform a search for "ain":</p>
<p id="demo"></p>
<script>
let text = "The rain in SPAIN stays mainly in the plain";
const myArr = text.match("ain");
document.getElementById("demo").innerHTML = myArr.length + " " + myArr;
</script>
</body>
</html>
Führen Sie eine Suche nach „ain“ durch:
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The match() Method</h2>
<p>Perform a search for "ain":</p>
<p id="demo"></p>
<script>
let text = "The rain in SPAIN stays mainly in the plain";
const myArr = text.match(/ain/);
document.getElementById("demo").innerHTML = myArr.length + " " + myArr;
</script>
</body>
</html>
Führen Sie eine globale Suche nach „ain“ durch:
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/g);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The match() Method</h2>
<p>Perform a global search for "ain":</p>
<p id="demo"></p>
<script>
let text = "The rain in SPAIN stays mainly in the plain";
const myArr = text.match(/ain/g);
document.getElementById("demo").innerHTML = myArr.length + " " + myArr;
</script>
</body>
</html>
Führen Sie eine globale Suche ohne Berücksichtigung der Groß- und Kleinschreibung nach „ain“ durch:
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/gi);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The match() Method</h2>
<p>Perform a global, case-insensitive search for "ain":</p>
<p id="demo"></p>
<script>
let text = "The rain in SPAIN stays mainly in the plain";
const myArr = text.match(/ain/gi);
document.getElementById("demo").innerHTML = myArr.length + " " + myArr;
</script>
</body>
</html>
Wenn ein regulärer Ausdruck den Modifikator g nicht enthält (globale Suche), match()
gibt nur die erste Übereinstimmung in der Zeichenfolge zurück.
Lesen Sie mehr über reguläre Ausdrücke im Kapitel JS RegExp.
Die Methode matchAll()
gibt einen Iterator zurück, der die Ergebnisse des Abgleichs enthält eine Zeichenfolge gegen eine Zeichenfolge (oder einen regulären Ausdruck).
const iterator = text.matchAll("Cats");
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The matchAll() Method</h2>
<p>ES2020 intoduced the string method matchAll().</p>
<p id="demo"></p>
<script>
let text = "I love cats. Cats are very easy to love. Cats are very popular."
const iterator = text.matchAll("Cats");
document.getElementById("demo").innerHTML = Array.from(iterator);
</script>
</body>
</html>
Wenn der Parameter ein regulärer Ausdruck ist, muss andernfalls das globale Flag (g) gesetzt werden Es wird ein TypeError ausgelöst.
const iterator = text.matchAll(/Cats/g);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The matchAll() Method</h2>
<p>ES2020 intoduced the string method matchAll().</p>
<p id="demo"></p>
<script>
let text = "I love cats. Cats are very easy to love. Cats are very popular."
const iterator = text.matchAll(/Cats/g);
document.getElementById("demo").innerHTML = Array.from(iterator);
</script>
</body>
</html>
Wenn Sie bei der Suche die Groß-/Kleinschreibung ignorieren möchten, muss das Insensitive-Flag (i) gesetzt sein:
const iterator = text.matchAll(/Cats/gi);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The matchAll() Method</h2>
<p>ES2020 intoduced the string method matchAll().</p>
<p id="demo"></p>
<script>
let text = "I love cats. Cats are very easy to love. Cats are very popular."
const iterator = text.matchAll(/Cats/gi);
document.getElementById("demo").innerHTML = Array.from(iterator);
</script>
</body>
</html>
matchAll()
ist eine ES2020-Funktion.
matchAll()
funktioniert nicht im Internet Explorer.
Die Methode includes()
gibt true zurück, wenn eine Zeichenfolge einen angegebenen Wert enthält.
Andernfalls wird false
zurückgegeben.
Überprüfen Sie, ob eine Zeichenfolge „world“ enthält:
let text = "Hello world, welcome to the universe.";
text.includes("world");
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The includes() Method</h2>
<p>Check if a string includes "world":</p>
<p id="demo"></p>
<p>The includes() method is not supported in Internet Explorer.</p>
<script>
let text = "Hello world, welcome to the universe.";
document.getElementById("demo").innerHTML = text.includes("world");
</script>
</body>
</html>
Überprüfen Sie, ob eine Zeichenfolge „world“ enthält. Beginnen Sie bei Position 12:
let text = "Hello world, welcome to the universe.";
text.includes("world", 12);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The includes() Method</h2>
<p>Check if a string includes "world" starting from position 12:</p>
<p id="demo"></p>
<p>The includes() method is not supported in Internet Explorer.</p>
<script>
let text = "Hello world, welcome to the universe.";
document.getElementById("demo").innerHTML = text.includes("world", 12);
</script>
</body>
</html>
Bei includes()
wird die Groß-/Kleinschreibung beachtet.
includes()
ist eine ES6-Funktion.
includes()
wird in Internet Explorer nicht unterstützt.
Die Methode startsWith()
gibt true
zurück wenn eine Zeichenfolge mit einem angegebenen Wert beginnt.
Andernfalls wird false
zurückgegeben:
Gibt true zurück:
let text = "Hello world, welcome to the universe.";
text.startsWith("Hello");
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The startsWith() Method</h2>
<p>Check if a string starts with "Hello":</p>
<p id="demo"></p>
<p>The startsWith() method is not supported in Internet Explorer.</p>
<script>
let text = "Hello world, welcome to the universe.";
document.getElementById("demo").innerHTML = text.startsWith("Hello");
</script>
</body>
</html>
Gibt false zurück:
let text = "Hello world, welcome to the universe.";
text.startsWith("world")
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The startsWith() Method</h2>
<p id="demo"></p>
<p>The startsWith() method is not supported in Internet Explorer.</p>
<script>
let text = "Hello world, welcome to the universe.";
document.getElementById("demo").innerHTML = text.startsWith("world");
</script>
</body>
</html>
Eine Startposition für die Suche kann angegeben werden:
Gibt false zurück:
let text = "Hello world, welcome to the universe.";
text.startsWith("world", 5)
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The startsWith() Method</h2>
<p id="demo"></p>
<p>The startsWith() method is not supported in Internet Explorer.</p>
<script>
let text = "Hello world, welcome to the universe.";
document.getElementById("demo").innerHTML = text.startsWith("world", 5);
</script>
</body>
</html>
Gibt true zurück:
let text = "Hello world, welcome to the universe.";
text.startsWith("world", 6)
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The startsWith() Method</h2>
<p id="demo"></p>
<p>The startsWith() method is not supported in Internet Explorer.</p>
<script>
let text = "Hello world, welcome to the universe.";
document.getElementById("demo").innerHTML = text.startsWith("world", 6);
</script>
</body>
</html>
Bei startsWith()
wird die Groß-/Kleinschreibung beachtet.
startsWith()
ist eine ES6-Funktion.
startsWith()
wird in Internet Explorer nicht unterstützt.
Die Methode endsWith()
gibt true
zurück wenn eine Zeichenfolge mit einem angegebenen Wert endet.
Andernfalls wird false
zurückgegeben:
Prüfen Sie, ob eine Zeichenfolge mit „Doe“ endet:
let text = "John Doe";
text.endsWith("Doe");
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Strings</h2>
<p>Check if a string ends with "Doe":</p>
<p id="demo"></p>
<p>The endsWith() method is not supported in Internet Explorer.</p>
<script>
let text = "John Doe";
document.getElementById("demo").innerHTML = text.endsWith("Doe");
</script>
</body>
</html>
Prüfen Sie, ob die ersten 11 Zeichen einer Zeichenfolge mit „world“ enden:
let text = "Hello world, welcome to the universe.";
text.endsWith("world", 11);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Strings</h2>
<p>Check in the 11 first characters of a string ends with "world":</p>
<p id="demo"></p>
<p>The endsWith() method is not supported in Internet Explorer.</p>
<script>
let text = "Hello world, welcome to the universe.";
document.getElementById("demo").innerHTML = text.endsWith("world", 11);
</script>
</body>
</html>
Bei endsWith()
wird die Groß-/Kleinschreibung beachtet.
endsWith()
ist eine ES6-Funktion.
endsWith()
wird in Internet Explorer nicht unterstützt.
Eine vollständige String-Referenz finden Sie in unserem:
Vollständige JavaScript-String-Referenz.
Die Referenz enthält Beschreibungen und Beispiele aller String-Eigenschaften und -Methoden.