Sie sind auf Seite 1von 4

Regular Expression with DreamWeaver

Table 1. Commonly used meta-characters:


Meta-character Matches
. Any single character, except a new-line
\d Any digit character (0-9)
\D Any non-digit character—in other words, anything except 0-9
\w Any alphanumeric character or the underscore
\W Any character, except an alphanumeric character or the underscore
\s Any white-space character, including space, tab, form feed, or line feed
\S Any character, except a white-space character
\f A form feed character
\n A line feed character
\r A carriage return character
\t A tab character
With the exception of the dot (or period), all the wildcard character sequences begin with a backslash. The
dot meta-character matches anything, including a space, punctuation mark, or even itself. The only thing it
doesn't match is a new-line. This is a common source of mistakes when composing regular expressions.

Table 2. Quantifiers used to repeat a pattern


Quantifier Meaning
* Match 0 or more times
+ Match 1 or more times
? Match no more than once (makes the character or group optional)
{n} Match exactly n times
{n,m} Match at least n, but no more than m times
*? Match 0 or more times, but as few times as possible
+? Match 1 or more times, but as few times as possible
?? Match 0 or 1 times, but as few times as possible
{n}? Match at least n times, but as few times as possible
{n,m}? Match at least n times, no more than m times, and as few times as possible

Table 3. Meta-characters for word boundaries, anchors, and alternation


Character/sequence Meaning
\b Match word boundary
\B Match word non-boundary
^ Match beginning of subject text or line
$ Match end of subject text or line
| Alternate pattern
Regular Expression with DreamWeaver

Character classes provide you with a way to restrict the characters you are searching for to a certain set by
wrapping those characters in brackets.
Character Matches Example
[abc] Any one of the characters enclosed in the brackets. [e-g] matches "e" in "bed", "f" in "folly",
Specify a range of characters with a hyphen (for and "g" in "guard"
example, [a-f] is equivalent to [abcdef]).
[^abc] Any character not enclosed in the brackets. Specify [^aeiou] initially matches "r" in "orange",
a range of characters with a hyphen (for example, "b" in "book", and "k" in "eek!"
[^a-f] is equivalent to [^abcdef]).
\b A word boundary (such as a space or carriage \bb matches "b" in "book" but nothing in
return). "goober" or "snob"
\B Anything other than a word boundary. \Bb matches "b" in "goober" but nothing in
"book"
\d Any digit character. Equivalent to [0-9]. \d matches "3" in "C3PO" and "2" in
"apartment 2G"
\D Any nondigit character. Equivalent to [^0-9]. \D matches "S" in "900S" and "Q" in "Q45"
\w Any alphanumeric character, including underscore. b\w* matches "barking" in "the barking
Equivalent to [A-Za-z0-9_]. dog" and both "big" and "black" in "the big
black dog"
\W Any non-alphanumeric character. Equivalent to [^A- \W matches "&" in "Jake&Mattie" and "%"
Za-z0-9_]. in "100%"
\s Any single white-space character, including space, \sbook matches "book" in "blue book" but
tab, form feed, or line feed. nothing in "notebook"
\S Any single non“white-space character. \Sbook matches "book" in "notebook" but
nothing in "blue book"
\f A form feed character. --
\n A line feed character. --
\r A carriage return character.
Note: Dreamweaver MX 2004 contains a bug in its regular expression engine where carriage return characters (\r)
are not recognized when you click the Find Next button. However, clicking the Find All button does reveal these
characters.
\t A tab character. --
Regular Expression with DreamWeaver

Special characters, sometimes referred to as meta-characters, are reserved, non-alphanumeric


characters that provide special types of functionality.
Character Matches Example
^ Beginning of input or line ^T matches "T" in "This good earth" but not in "Uncle
Tom's Cabin"
$ End of input or line h$ matches "h" in "teach" but not in "teacher"
* The preceding character 0 or um* matches "um" in "rum", "umm" in "yummy", and "u"
more times in "huge"
+ The preceding character 1 or um+ matches "um" in "rum" and "umm" in "yummy" but
more times nothing in "huge"
? The preceding character at most st?on matches "son" in "Johnson" and "ston" in
once (that is, indicates that the "Johnston" but nothing in "Appleton" or "tension"
preceding character is optional)
. Any single character except .an matches "ran" and "can" in the phrase "bran muffins
newline can be tasty"
X|y Either x or y FF0000|0000FF matches "FF0000" in
bgcolor="#FF0000" and "0000FF'" in font
color="#0000FF"
{n} Exactly n occurrences of the o{2} matches "oo" in "loom" and the first two o's in
preceding character "mooooo" but nothing in "money"
{n,m} At least n, and at most m, F{2,4} matches "FF" in "#FF0000" and the first four F's
occurrences of the preceding in #FFFFFF
character
Note: The above table was originally published in the Dreamweaver product help (Help > Using Dreamweaver).
Regular Expression with DreamWeaver

JavaScript
There are two ways of creating a regex in JavaScript: using literal syntax or the RegExp() constructor.

Using literal syntax


To use literal syntax, wrap the regex in forward slashes, and assign it to a variable like this:
var phone = /\(?(\d{3})\)? (\d{3})[- ](\d{4})/;

The forward slashes are delimiters indicating that everything between them should be treated as a regex. If
the regex needs to match a literal forward slash, escape it with a backslash. For example, the regex for a
CSS comment needs to be rewritten like this:
var css_comment = /\/\*.*\*\//;

Using the RegExp() constructor


The RegExp() constructor requires the regex without delimiters as a string in single or double quotes.
This avoids the need to escape forward slashes, but introduces a different complication, which is arguably
worse. All backslashes need to be escaped. For example, instead of using \d and \w, you need to use \\d and
\\w. The CSS comment regex looks like this:
var css_comment = new RegExp("/\\*.*\\*/");

Modifying a regex with flags


You can modify a regex by specifying one or more flags. JavaScript supports the three flags listed in Table
1.

Table 1. Flags supported by JavaScript to modify a regex's behavior


Flag Meaning
i Perform case-insensitive matching.
g Match all instances, rather than just the first one.
m Make ^ and $ match the beginning and end of lines, as well as the beginning and end of
the subject text.

When using literal syntax, add the flag(s) after the closing delimiter like this:
var css_comment = /\/\*.*\*\//gm;

When using the RegExp() constructor, pass the flags in a string as the second argument like this:
var css_comment = new RegExp("/\\*.*\\*/", "gm");

Das könnte Ihnen auch gefallen