Java Reference
In-Depth Information
•
+
matches one or more occurrences of the pattern
•
{n}
matches
n
occurrences of the pattern
•
{n,}
matches at least
n
occurrences of the pattern
•
{,m}
matches at most
m
occurrences of the pattern
•
{n,m}
matches at least
n
and at most
m
occurrences of the pattern
•
^
specifies that the pattern must come at the beginning
•
$
specifies that the pattern must come at the end
Any special characters or modifiers can be escaped using a backslash. So if you wanted to
match a question mark,
?
, you would need to use the regular expression
/\?/
.
For example, the following regular expression will match anything that starts with
J
fol-
lowed by one or more vowels, then any letters or numbers ending in
ing
:
var pattern = /J[aeiou]+\w*ing/
As we can see, it matches the words "
Joking
" and "
Jeering
":
pattern.test("Joking");
<< true
pattern.test("Jeering");
<< true
A Practical Example
If we were looking for PDF files and had a list of filenames, this regular expression could
be used to find them (assuming they have a .pdf extension, of course):
var pdf = /.*\.pdf$/;
