1. \d,\w,\s,[a-zA-Z0-9],\b,.,*,+,?,x{3},^$分别是什么?
- \d 查找数字
"12 aa bCD -AD_ +-8/@!".match(/\d/g)//["1","2","8"]
- \w 查找单词字符(包括英文字母,数字,下划线)
"12aabCDAD_+-8/@!".match(/\w/g)//["1","2","a","a","b","C","D","A","D","_","8"]
- \s 查找空白字符(空格、tab、换行、回车)
"a ab cd".match(/\s/g)//[" "," "]
- [a-zA-Z0-9] 查找0-9、a-z、A-Z的字符,和\w相比少了一个下划线,从正则的分类来说,一个属于方括号类型,一个属于元字符类型
"aa091+Ab_".match(/[0-9a-zAZ]/g)//['a','a','0','9','1','A','b']
- \b 匹配单词边界(开头、结尾、空格分隔、中横线分隔)
"hello hello-world".match(/\bhell/g)//["hell","hell"]
"hello world-hello".match(/\bhellb/g)//["hell"]
- . :匹配单个字符,除了换行和行结束符
"hello 01\n".match(/./)//["h"]
- *:匹配匹配零个或多个
"ab abb abbb b".match(/ab*/g)//['ab','abb','abbb']
- +: 匹配一个或多个
"ab abb abbb b".match(/ab+/g)//["ab", "abb", "abbb"]
- ?: 匹配零个或一个(非贪婪模式)
"ab abb abbb b".match(/ab?/g)//["ab", "ab", "ab"]
- x{3}:匹配包含3个x的字符串
"ax1 axx2 axxx3 x4".match(/ax{3}/g)//["axxx"]
- ^$:匹配空字符串(连一个空格也没有的)
"".match(/^$/g)//['']
" ".match(/^$/g)//null
2.贪婪模式和非贪婪模式指什么?
- 贪婪匹配:正则表达式一般趋向于最大长度匹配,也就是所谓的贪婪匹配
"ab abb abbb b".match(/ab*/g)//["ab","abb","abbb"]
- 非贪婪匹配:就是匹配到结果就好,就少的匹配字符
"ab abb abbb b".match(/ab*/g)//["ab","ab","ab"]
- 编程中如何区分两种模式-默认是贪婪模式;在量词后面直接加上一个问号?就是非贪婪模式。
-以下是详细 的正则表达式常用匹配字符
1.修饰符

2.方括号

3.元字符

![R]OEQZ`EY_U{W10EL~RQ27E.png](https://img.haomeiwen.com/i3361706/f5a4424c19fe70f8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
4.量词


网友评论