记忆表
几个简单的例子(JavaScript)
1. + 的用法
var pattern = /ab+g/;
串 | 结果 |
"dfeoabg" | "abg" |
"dfeoabfg" | null |
"dfeoabbg" | "abbg" |
"dfeoab" | null |
2. ? 的用法
var pattern = /ab?g/;
串 | 结果 |
"dfeoag" | "ag" |
"dfeoabg" | "abg" |
"dfeoabbg" | null |
"dfeobg" | null |
3. (?:) 的用法
var text = "mom and dad and baby";
模式 | 结果 |
/mom ( and dad ( and baby))/ | ["mom and dad and baby", " and dad and baby", " and baby", index: 0, input: "mom and dad and baby"] |
/mom ( and dad (?: and baby))/ | ["mom and dad and baby", " and dad and baby", index: 0, input: "mom and dad and baby"] |
/mom(?: and dad( and baby))/ | ["mom and dad and baby", " and baby", index: 0, input: "mom and dad and baby"] |
对于正则表达式
( and baby) ,() 里的内容被捕获,反向引用时可以用上
而
(?: and baby) 可以简单理解成
?: 关闭了圆括号的捕获能力,只用其来分组,所以不会占用捕获变量,也不能使用反向引用。
关于捕获组与反向引用可参见:
http://blog.csdn.net/lxcnn/article/details/4146148