Java Reference
In-Depth Information
var pattern = /Java/; // A non-global pattern
var text = "Java and JavaScript are not the same";
var result;
/* Do not use this loop. It will run forever because
pattern is non-global.
*/
while((result = pattern.exec(text)) !== null) {
// Code goes here
}
Some characters when used in certain contexts have special meaning. These
characters are known as metacharacters and they are
(, )
,
[
,
]
,
{
,
}
,
\
,
^
,
$
,
|
,
?
,
*
,
+
, and
the dot. I will discuss the special meanings of these characters at appropriate places in
this section.
In a regular expression, a set of characters enclosed within a pair of brackets is known
as a character class. The regular expression will match any of the characters in the set.
For example,
/[abc]/
will match
a
,
b
, or
c
in the input text. You can also specify a range of
characters using a character class. The range is expressed using a hyphen. For example,
[a-z]
represents any lowercase English letters;
[0-9]
represents any digit between 0 and 9.
If you use the character
^
in the beginning of a character class, it means complement
(meaning not). For example,
[^abc]
means any character except
a
,
b
, and
c
. The character
class
[^a-z]
represents any character except lowercase English letters. If you use the
character
^
anywhere in a character class, except in the beginning, it loses the special
meaning and it matches just a
^
character. For example,
[abc^]
will match
a
,
b
,
c
, or
^
.
Character classes also support multiple ranges and operations such as union, intersection,
subtraction on those ranges. Table
4-18
shows a few examples of using ranges in character
classes. Nashorn has some predefined character classes as listed in Table
4-19
.
Table 4-18.
A Few Examples of Character Classes
Character Classes
Meaning
Category
[abc]
Character
a
,
b,
or
c
Simple character class
[^xyz]
A character except
x
,
y,
and
z
Complement or negation
[a-p]
Characters
a
through
p
Range
[a-cx-z]
Characters
a
through
c
, or
x
through
z
,
which would include
a
,
b
,
c
,
x
,
y
, or
z
.
Union
[0-9&&[4-8]]
Intersection of two ranges (
4
,
5
,
6
,
7
, or
8
)
Intersection
[a-z&&[^aeiou]]
All lowercase letters minus vowels. In
other words, a lowercase letter that
is not a vowel. That is, all lowercase
consonants.
Subtraction
Search WWH ::
Custom Search