Visit Mozilla.org

Dokumentacja języka JavaScript 1.5:Obiekty:String:match

z Mozilla Developer Center, polskiego centrum programistów Mozilli.

UWAGA: Tłumaczenie tej strony nie zostało zakończone.
Może być ona niekompletna lub wymagać korekty.
Chcesz pomóc? | Dokończ tłumaczenie | Sprawdź ortografię | Więcej takich stron...

Spis treści

[edytuj] Podsumowanie

Used to retrieve the matches when matching a string against a regular expression.

Metoda obiektu: String
Implemented in: JavaScript 1.2
Wersja ECMA: ECMA-262, Edition 3

[edytuj] Składnia

match(regexp)

[edytuj] Parametry

regexp 
Obiekt wyrażenia regularnego. If a non-RegExp object obj is passed, it is implicitly converted to a RegExp by using new RegExp(obj).

[edytuj] Opis

If the regular expression does not include the g flag, returns the same result as regexp.exec(string).

If the regular expression includes the g flag, the method returns an Array containing all matches.

[edytuj] Uwagi

[edytuj] Additional Reading

  • See §15.5.4.10 of the ECMA-262 specification.

[edytuj] Przykłady

[edytuj] Przykład: Zastosowanie match

In the following example, match is used to find "Chapter" followed by 1 or more numeric characters followed by a decimal point and numeric character 0 or more times. The regular expression includes the i flag so that case will be ignored.

<SCRIPT>
str = "For more information, see Chapter 3.4.5.1";
re = /(chapter \d+(\.\d)*)/i;
found = str.match(re);
document.write(found);
</SCRIPT>

This returns the array containing Chapter 3.4.5.1,Chapter 3.4.5.1,.1

"Chapter 3.4.5.1" is the first match and the first value remembered from (Chapter \d+(\.\d)*).

".1" is the second value remembered from (\.\d).

[edytuj] Przykład: Using global and ignore case flags with match

The following example demonstrates the use of the global and ignore case flags with match. All letters A through E and a through e are returned, each its own element in the array

var string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var regexp = /[A-E]/gi;
var matches_array = str.match(regexp);
document.write(matches_array);

matches_array now equals ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']