兩種定義Regex對象(RegExp)的方法:
1) var pattern = /s$/;
2) var pattern = new RegExp("s$");
系統學習Regex的兩本參考:
1)Programming Perl by Larry Wall et al. (O'Reilly).
2)Mastering Regular Expressions by Jeffrey E.F. Friedl (O'Reilly)
逸出字元(backslash):\
字母和數字不需要轉義。其他字元如果記不住就用吧。
這些字元有特殊含義,需要轉義:^ $ . * + ? = ! : | \ / ( ) [ ] { }
其它特殊字元表示方法:
\0 NULL(unicode十六進位標記法為\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 hexadecimal number nn; for example, \x0A is the same as \n
\uxxxx The Unicode character specified by the hexadecimal 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
一類字元的表示方法:
[...] 括弧內的任意一個字元
[^...] 除括弧內字元之外的任意一個字元
. 除分行符號(or another Unicode line terminator)外的任意一個字元
\w 字母、數字或底線,等價於[a-zA-Z0-9_]
\W 除字母、數字或底線外的任意一個字元,等價於[^a-zA-Z0-9_].
\s Unicode whitespace
\S 除Unicode whitespace之外的其他字元。須注意\w和\S是不一樣的。
\d 數字,等價於[0-9]
\D 非數字,等價於[^0-9].
[\b] backspace (方括弧之間的\b指的是鍵盤上Backspace鍵對應的字元).
\b \w和\W之間的位置(錨點),請注意第二個W是大寫字母。