Archive for April 17, 2007

JavaScript Regular Expression Backslash

Posted in JavaScript, Software, Web, programming on April 17, 2007 by Joey

The backslash (\) has special meaning in JavaScript regular expressions. It’s used before certain characters as a way to provide a special meaning. For example, the /t/ as a regular expression matches itself literally. If it is preceded by the backslash character in the regular expression, such as /\t/, the regular expression would match a tab.

The general regular expression backslash usages are listed below:

\t = tab
\n = newline
\f = form feed
\r = carriage return
\w = any ASCII word character
\W = any character that is not an ASCII word character
\d = any ASCII digit
\D = any character that is not an ASCII digit
\s = any whitespace character
\S = any character that is not a whitespace character

The backslash character can also be used in front of characters that are used specially in regular expressions as a way to match those characters literally. For example, the dollar sign ($) is used in regular expressions to specify the end of a pattern. To match it literally, precede it with a backslash, such as /\$/.

Related Articles:
JavaScript Regular Expressions