Signatur
Beschreibung
Die Methode match() von String-Werten liefert das Ergebnis des Abgleichs dieses Strings mit einem regulären Ausdruck.
Die Implementierung von String.prototype.match tut wenig mehr, als die Symbol.match-Methode des Arguments mit dem String als erstem Parameter aufzurufen. Die eigentliche Implementierung stammt aus RegExp.prototype[Symbol.match]().
- Wenn Sie wissen möchten, ob ein String zu einem regulären Ausdruck
RegExppasst, verwenden SieRegExp.prototype.test(). - Wenn Sie nur den ersten gefundenen Treffer wollen, sollten Sie stattdessen
RegExp.prototype.exec()verwenden. - Wenn Sie Capture Groups erhalten möchten und das globale Flag gesetzt ist, müssen Sie stattdessen
RegExp.prototype.exec()oderString.prototype.matchAll()verwenden.
Weitere Informationen zur Semantik von match(), wenn ein Regex übergeben wird, finden Sie unter RegExp.prototype[Symbol.match]().
Parameter
| Name | Typ | Default | Beschreibung |
|---|---|---|---|
| $regexp Pflicht | RegExp | Object | — | Ein reguläres Ausdrucksobjekt oder jedes Objekt, das eine Symbol.match-Methode besitzt. Wenn regexp kein RegExp-Objekt ist und keine Symbol.match-Methode besitzt, wird es implizit mittels new RegExp(regexp) in ein RegExp umgewandelt. Wird kein Parameter übergeben und match() direkt verwendet, erhält man ein Array mit einem leeren String: [""], da dies gleichwertig zu match(/(?:)/) ist. |
Rückgabewert
Beispiele
match() verwenden
const str = "For more information, see Chapter 3.4.5.1";
const re = /see (chapter \d+(\.\d)*)/i;
const found = str.match(re);
console.log(found);
// [
// 'see Chapter 3.4.5.1',
// 'Chapter 3.4.5.1',
// '.1',
// index: 22,
// input: 'For more information, see Chapter 3.4.5.1',
// groups: undefined
// ]
Die Flags global und ignoreCase mit match() verwenden
const str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const regexp = /[a-e]/gi;
const matches = str.match(regexp);
console.log(matches);
// ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
Benannte Capturing Groups verwenden
const paragraph = "The quick brown fox jumps over the lazy dog. It barked.";
const capturingRegex = /(?<animal>fox|cat) jumps over/;
const found = paragraph.match(capturingRegex);
console.log(found.groups); // {animal: "fox"}
match() ohne Parameter verwenden
const str = "Nothing will come of nothing.";
str.match(); // returns [""]
match() mit einem Nicht-RegExp, das [Symbol.match]() implementiert
const str = "Hmm, this is interesting.";
str.match({
[Symbol.match](str) {
return ["Yes, it's interesting."];
},
}); // returns ["Yes, it's interesting."]
Ein Nicht-RegExp als Parameter
const str1 =
"All numbers except NaN satisfy <= Infinity and >= -Infinity in JavaScript.";
const str2 =
"My grandfather is 65 years old and My grandmother is 63 years old.";
const str3 = "The contract was declared null and void.";
str1.match("number"); // "number" is a string. returns ["number"]
str1.match(NaN); // the type of NaN is the number. returns ["NaN"]
str1.match(Infinity); // the type of Infinity is the number. returns ["Infinity"]
str1.match(-Infinity); // returns ["-Infinity"]
str2.match(65); // returns ["65"]
str3.match(null); // returns ["null"]
Unerwartete Ergebnisse ohne Escaping
console.log("123".match("1.3")); // [ "123" ]
Korrektes Escaping
console.log("123".match("1\\.3")); // null