Pattern Matching with Regular Expressions
Defining
In JavaScript, regular expressions are accomplished by using RegExp objects. You can either define a regular expression with the RegExp() constructor, or by using the more common syntax:
<code>
var pattern = /s$/;
var pattern = new RegExp(“s$”);
</code>
===== Literal Characters =====
^Character ^Matches ^
|Alphanumeric character |Itself |
|\0 |The NUL character (\u0000) |
|\t |Tab (\u0009) |
|\n |Newline (\u000A) |
|\v |Vertical tab (\u000B) |
|\f |Form feed (\u000C) |
|\r |Carriage return (\u000D) |
|\xnn |The Latin character specified by the hexidecimal number nn; for example, \x0A is the same as \n |
|\uxxxx |The Unicode character specified by the hexidecimal number xxxx; for example, \u0009 is the same as \t |
|\cX |The control character ^X
; for example, \cJ is equivalent to the newline character \n |
===== Punctuation characters with special meaning =====
<code>
^ $ . * + ? = ! : | \ / ( ) [ ] { }
</code>
===== Character Classes =====
^Character ^Matches ^
|[…] |Any one character between the brackets |
|[^
…] |Any one character not between the brackets |
|. |Any character except newline or another Unicode line terminator |
|\w |Any ASCII word character; Equivalent to [a-zA-Z0-9_] |
|\W |Any character that is not an ASCII word character; Equivalent to [^
a-zA-Z0-9_] |
|\s |Any Unicode whitespace character |
|\S |Any character that is not Unicode whitespace; Note that \w and \S are not the same thing |
|\d |Any ASCII digit; Equivalent to [0-9] |
|\D |Any character other than an ASCII digit; Equivalent to [^
0-9] |
|[\b] |A literal backspace (special case) |
===== Repetition =====
^Character ^Meaning ^
|{n,m} |Match the previous item at least n times but no more than m times |
|{n,} |Match the previous item n or more times |
|{n} |Match exactly n occurences of the previous item |
|? |Match zero or one ocurrences of the previous item. That is, the previous item is optional. Equivalent to {0,1}. |
|+ |Match one or more occurrences of the previous item. Equivalent to {1,} |
|* |Match zero or more occurrences of the previous item. Equivalent to {0,} |
- enter more table references