Regular Expressions

Flashcard 5 of 10

How can this pattern be simplified?

/[0-9]+[ \t\r\n\f][a-zA-Z0-9_]*/

We can use metacharacters to replace and simplify our character classes:

Metacharacters are built-in character classes. The \d metacharacter is the same as the [0-9] character class, \s is the same as [ \t\r\n\f], and \w is the same as [a-zA-Z0-9_]. With these in mind, we can rewrite our original pattern much more concisely as:

/\d+\s\w*/

The full list of metacharacters as listed in the character classes section of the regex class docs is:

Metacharacter Interpretation
. Any character except a newline
\w A word character ([a-zA-Z0-9_])
\W A non-word character ([^a-zA-Z0-9_])
\d A digit character ([0-9])
\D A non-digit character ([^0-9])
\h A hex digit character ([0-9a-fA-F])
\H A non-hex digit character ([^0-9a-fA-F])
\s A whitespace character: ([ \t\r\n\f])
\S A non-whitespace character: ([^ \t\r\n\f])
Return to Flashcard Results