JavaScript String match()
Příklady
Hledání "ain" pomocí řetězce:
let text = "The rain in SPAIN stays mainly in the plain";
text.match("ain");
Hledání „ain“ pomocí regulárního výrazu:
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/);
Globální hledání „ain“:
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/g);
Globální vyhledávání bez rozlišení velkých a malých písmen:
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/gi);
Definice a použití
Metoda match()
porovnává řetězec s regulárním výrazem **
Metoda match()
vrací pole se shodami.
Pokud není nalezena žádná shoda, metoda match()
vrátí hodnotu null .
Poznámka
** Pokud je hledanou hodnotou řetězec, převede se na regulární výraz.
Viz také:
Syntax
string.match(match)
Parametry
Parameter | Description |
match | Required. The search value. A regular expression (or a string that will be converted to a regular expression). |
Návratové hodnoty
Typ | Popis |
Pole resp null | Pole obsahující shody. null pokud není nalezena žádná shoda. |
Rozdíl mezi
String match() a String search()
Metoda match()
vrací pole shod.
Metoda search()
vrací pozici první shody.
Metody vyhledávání regulárních výrazů
V JavaScriptu lze textové vyhledávání regulárních výrazů provádět různými metodami.
Se vzorem jako regulárním výrazem jsou nejběžnější metody:
Příklad | Popis |
---|---|
text.match( vzor ) | Metoda String match() |
text.search( vzor ) | Metoda String search() |
vzor .exec(text) | Metoda RexExp exec() |
vzor .test(text) | Metoda RegExp test() |
Podpora prohlížeče
match()
je funkce ECMAScript1 (ES1).
ES1 (JavaScript 1997) je plně podporován ve všech prohlížečích:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |