Javascript Reglar Expressions
4685 ワード
10.1.2 Charcter Class es
Individual lighteral characters can be commbined into character clases by placing them within square bractes.A character class any one character that is contained within it.Thus,the reglar exprestion/abc therseor c.Negated character classes can also be defined--these match any character except those contained within the bractes.A negated character class specified by caret(^as)as the first characthers.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.aor c.Charcter classies can use a hyphen to indicate a range of characters.To match any one lowerscase from the Latin alphablet,use/[a-z]/,and to match any letter digit from the Latin alphablet,ze[a-ze]
Because certain character clases are commonly used、the JavaScript reglar expression sysntax includes special character s and escape sequence to represent these common clases.For example、s thers special theracterand\S matches any character that is not Unicode whitepace.Table 10-2 lists these characters and summares character class sntax.(Note that several of these character class escape sequences match only ASCII characters and have not been exted to work with Unicode characters.You can explicitly define your own Unicode Unicoracter clast;
Table 10-2.Reglar expressition character clases
Chracter
マッチ
[…]
Any one character between the braackets.
[^…]
Any one character not between the braackets.
..
Any character except newline or another Unicode line terminator.
\w
Any ASCII word character.Equivalent to[a-zA-Z 0-9_]
\W
Any character that is not an ASCII word character.Equivalent to[^a-zA-Z 0-9_]
\s
Any Unicode whites pace character.
\S
Any character that is not Unicode whitess pace.Note that\w and\S are not the same thing.
\d
Any ASCII digit.Equivalent to[0-9]
\D
Any character other than ASCII digit.Equivalent to[^0-9].
[\b]
A literal backspace(special case)
Note that the special character class capes can be used within square braackets.\s matches any whitch character and\d matches any digit,so/s\dmatches any one whitotonenewhitpace character or digit.Note that there isone special case.As we'll see later、the\b escape has a special meaning.When used with in a character class、however、it it represents thethethepapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapanese、shshshshshshshshshs.shshse、Howese、Howee e e e e e e e e th one element:/[\b]/.
10.1.3 Repetition
With the reglar expressisisisisisisisision sntax we have learned so far、we can describe a two-digit number as//d/anda four-digit number/d\d\d\d\d/d/d/d/But we t wewewedon't have't have have have have aaawawawawawave dededededededededededededededededededededededededededededededededededededededededededededededededededededededededededededededededededededededededededeoptional digit.The e more complex patterns use reglar expressitionn syntax that specifes how many times an element of a reglar expression may be repeat.
The characters that specify repetition alwaysフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォネットネットネットtheaah ararararararararararararararararararararararaaaaaaaaaarararararararararararararararararaaaaaaaaaaaaarerererererererererererereinininininininininininininininininininininininax.The follwing lines show some examples:
Chracter
Meaning
{n,m}
Match the previous item least n times butのmore than m times.
{n,}
Match the previous item n or more times.
{n}
Match exactly n occurrences of the previous item.
?
Matchゼロor one occurrences of the previous item.That is,the previous item is optional.Equivalent to{0,1}
+
Match one or more occurrences of the previous item.Equivalent to{1,}
*
Matchゼロor more occurrences of the previous item.Equivalent to{0,}
Be careful when using the*and?repetition characters.Since these characters may may may mashers of whatever precedies them,they are allowed to match nothing.For example,the reglar expression/a
Individual lighteral characters can be commbined into character clases by placing them within square bractes.A character class any one character that is contained within it.Thus,the reglar exprestion/abc therseor c.Negated character classes can also be defined--these match any character except those contained within the bractes.A negated character class specified by caret(^as)as the first characthers.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.atch.aor c.Charcter classies can use a hyphen to indicate a range of characters.To match any one lowerscase from the Latin alphablet,use/[a-z]/,and to match any letter digit from the Latin alphablet,ze[a-ze]
Because certain character clases are commonly used、the JavaScript reglar expression sysntax includes special character s and escape sequence to represent these common clases.For example、s thers special theracterand\S matches any character that is not Unicode whitepace.Table 10-2 lists these characters and summares character class sntax.(Note that several of these character class escape sequences match only ASCII characters and have not been exted to work with Unicode characters.You can explicitly define your own Unicode Unicoracter clast;
Table 10-2.Reglar expressition character clases
Chracter
マッチ
[…]
Any one character between the braackets.
[^…]
Any one character not between the braackets.
..
Any character except newline or another Unicode line terminator.
\w
Any ASCII word character.Equivalent to[a-zA-Z 0-9_]
\W
Any character that is not an ASCII word character.Equivalent to[^a-zA-Z 0-9_]
\s
Any Unicode whites pace character.
\S
Any character that is not Unicode whitess pace.Note that\w and\S are not the same thing.
\d
Any ASCII digit.Equivalent to[0-9]
\D
Any character other than ASCII digit.Equivalent to[^0-9].
[\b]
A literal backspace(special case)
Note that the special character class capes can be used within square braackets.\s matches any whitch character and\d matches any digit,so/s\dmatches any one whitotonenewhitpace character or digit.Note that there isone special case.As we'll see later、the\b escape has a special meaning.When used with in a character class、however、it it represents thethethepapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapapanese、shshshshshshshshshs.shshse、Howese、Howee e e e e e e e e th one element:/[\b]/.
10.1.3 Repetition
With the reglar expressisisisisisisisision sntax we have learned so far、we can describe a two-digit number as//d/anda four-digit number/d\d\d\d\d/d/d/d/But we t wewewedon't have't have have have have aaawawawawawave dededededededededededededededededededededededededededededededededededededededededededededededededededededededededededededededededededededededededededeoptional digit.The e more complex patterns use reglar expressitionn syntax that specifes how many times an element of a reglar expression may be repeat.
The characters that specify repetition alwaysフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォフォネットネットネットtheaah ararararararararararararararararararararararaaaaaaaaaarararararararararararararararararaaaaaaaaaaaaarerererererererererererereinininininininininininininininininininininininax.The follwing lines show some examples:
/\d{2,4}/ // Match between two and four digits
/\w{3}\d?/ // Match exactly three word characters and an optional digit
/\s+java\s+/ // Match "java" with one or more spaces before and after
/[^"]*/ // Match zero or more non-quote characters
Table 10-3.Reglar expressition repetition charactersChracter
Meaning
{n,m}
Match the previous item least n times butのmore than m times.
{n,}
Match the previous item n or more times.
{n}
Match exactly n occurrences of the previous item.
?
Matchゼロor one occurrences of the previous item.That is,the previous item is optional.Equivalent to{0,1}
+
Match one or more occurrences of the previous item.Equivalent to{1,}
*
Matchゼロor more occurrences of the previous item.Equivalent to{0,}
Be careful when using the*and?repetition characters.Since these characters may may may mashers of whatever precedies them,they are allowed to match nothing.For example,the reglar expression/a