JavascriptRegex入門筆記

來源:互聯網
上載者:User

兩種定義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是大寫字母。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.