Welcome 微信登录

首页 / 脚本样式 / JavaScript / Javascript正则表达式入门笔记

Javascript正则表达式入门笔记2013-04-10 未知 两种定义正则表达式对象(RegExp)的方法:

1) var pattern = /s$/;

2) var pattern = new RegExp("s$");

系统学习正则表达式的两本参考:

1)Programming Perl by Larry Wall et al. (O"Reilly).

2)Mastering Regular Expressions by Jeffrey E.F. Friedl (O"Reilly)

转义字符(backslash):

字母和数字不需要转义。其他字符如果记不住就用吧。

这些字符有特殊含义,需要转义:^ $ . * + ? = ! : | / ( ) [ ] { }

其它特殊字符表示方法:

NULL(unicode十六进制表示法为u0000,下同)

Tab (u0009)

NewLine (u000A)

v Vertical tab (u000B)

f Form feed (u000C)

Carriage return (u000D)

xnn The Latin character specified by the hexadecimal number nn; for example, x0A is the same as

uxxxx The Unicode character specified by the hexadecimal number xxxx; for example, u0009 is the same as

cX The control character ^X; for example, cJ is equivalent to the newline character

一类字符的表示方法:

[...] 括号内的任意一个字符

[^...] 除括号内字符之外的任意一个字符

. 除换行符(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].

[] backspace (方括号之间的指的是键盘上Backspace键对应的字符).

 w和W之间的位置(锚点),请注意第二个W是大写字母。