两种方法构造regexp对象
var reg = /\bis\b /g
var reg = new regexp('\\bis\\b', 'g'
)
注:在linux中反斜线本身就是特殊字符,所以需要再次转义,故写成
\\bis\\b
的格式
示例
'he is a boy. is he?'.replace(/\bis\b/g, '0') // expected: "he 0 a boy. is he?" 'he is a boy. is he?'.replace(/\bis\b/i, '0') // expected: "he 0 a boy. 0 he?"
* + ? $ ^ . | \ ( ) { } [ }
元字符
字符 | 含义 |
---|---|
\t
|
水平制表符 |
\v
|
垂直制表符 |
\n
|
换行符 |
\r
|
回车符 |
\0
|
空字符 |
\f
|
换页符 |
\cx
|
与x对应的控制字符(ctrl+x) |
一般情况下正则表达式一个字符对应字符串的一个字符
[]
构建一个简单的类
[abc]
把字符串a或b或c归为一类,表达式可以匹配这类的字符
^
创建反向类/负向类
[^abc]
表示不是字符a或b或c的内容
示例
'a1b2c3d4'.replace(/[abc]/g, 'x'); //output: x1x2x3d4 'a1b2c3d4'.replace(/[^abc]/g, 'x'); //output: axbxcxxx
[a-z]
表示从a到z的任意字符
[]
中是可以连写的,例如[a-za-z]
表示从a到z大小写匹配
示例:
'a1b2c3d4'.replace(/[a-z]/g, 'q'); //output: q1q2q3q4 '2016-09-12'.replace(/[0-9-]/g, 'a'); //output: aaaaaaaaaa
字符 | 等价类 | 含义 |
---|---|---|
.
|
[^\r\n]
|
除了回车符和换行符之外的所有字符 |
\d
|
[0-9]
|
数字字符 |
\d
|
[^0-9]
|
非数字字符 |
\s
|
[\t\n\x0b\f\r]
|
空白字符 |
\s
|
[^\t\n\x0b\f\r]
|
非空白字符 |
\w
|
[a-za-z_0-9]
|
查询字符串(字母,数字下划线) |
\w
|
[^a-za-z_0-9]
|
非单词字符 |
字符 | 含义 |
---|---|
^
|
以xxx开头 |
$
|
以xxx结束 |
\b
|
单词边界 |
\b
|
非单词边界 |
示例
// for \b \b 'this is a boy'.replace(/is/g, '0'); //output: th0 0 a boy 'this is a boy'.replace(/\bis/g, '0'); //output: this 0 a boy 'this is a boy'.replace(/\bis/g, '0'); //output: th0 is a boy
// for ^ '@123@abc@'.replace(/@./g, 'q'); //output: q23qbc@ '@123@abc@'.replace(/^@./g, 'q'); //output: q23@abc@ '@123@abc@'.replace(/.@/g, 'q'); //output: @23qabcq '@123@abc@'.replace(/.@$/g, 'q'); //output: @123@abq
// for multip line mulstr="@123\n@456\n@789"; mulstr.replace(/^@\d/g, 'x'); //output:"x23\n@456\n@789" mulstr.replace(/^@\d/gm, 'x'); //output:"x23\nx456\nx789"
字符 | 含义 |
---|---|
?
|
出现零次或一次(最多出现一次) |
+
|
出现一次或多次(至少出现一次) |
*
|
出现零次或多次(任意次) |
{n}
|
出现n次 |
{n,m}
|
出现n到m次 |
{n,}
|
至少出现n次 |
\d{20} //匹配连续出现20次的数字 \d{20}\w\d?\w+\d*\d{3}\w{3,5}\d{3,}
?
即可。
使用量词:byron{3}
并不能完成目标。因为它只会对紧挨着的n
匹配三次,此时,需要使用分组。分组很简单,直接加上小括号即可。即(byron){3}
示例:
'a1b2c3d4'.replace(/[a-z]\d{3}/g, 'x') //a1b2c3d4 'a1b2c3d4'.replace(/([a-z]\d){3}/g, 'x') //xd4
'byroncaseper'.replace(/byron|caseper/g, 'x') //xx 'byronsperbyrcasper'.replace(/byr(on|ca)sper/g, 'x') //xx
$
进行捕获,然后再用$
进行表示。
示例
'2015-12-25'.replace(/(\d{4})-(\d{2})-(\d{2})/g, '$1/$2/$3') // 12/25/2015
?:
就可以了。例如
(?:bryon).(ok)
注:javascript不支持后顾,故不再演示
名称 | 正则 | 含义 |
---|---|---|
正向前瞻 |
exp(?=asset)
|
|
负向前瞻 |
exp(?!asset)
|
|
正向后顾 |
exp(?<=asset)
|
js不支持 |
负向后顾 |
exp(?<!asset)
|
js不支持 |
示例:
'a2*34v8'.replace(/\w(?=\d)/g, 'x') //"x2*x4x8" 'a2*34v8'.replace(/\w(?!\d)/g, 'x') //"ax*3xvx"
注:在test方法中,如果匹配全局后有多个结果时,lastindex保存上次执行结果,并向下迭代,直至没有结果时返回false,则重新回到首位。此时再次执行,则再次循环上面的步骤。
示例:
var reg1 = /\w/; var reg1 = /\w/g; while(reg2.test('ab')) { console.log(reg2.lastindex); } /* 1 2 */
var reg3 = /\d(\w)(\w)\d/; var reg4 = /\d(\w)(\w)\d/g; var ts = '$1az2bb3cy4dd5ee'; var ret = reg3.exec(ts); console.log(reg3.lastindex+'\t'+ret.index+'\t'+ret.tostring()); //"0 1 1az2,a,z" console.log(reg3.lastindex+'\t'+ret.index+'\t'+ret.tostring()); //"0 1 1az2,a,z" while(ret = reg4.exec(ts)) { console.log(reg4.lastindex + '\t'+ret.index+'\t'+ret.tostring()); } /* "5 1 1az2,a,z" "11 7 3cy4,c,y" */
'a1b2c3d4'.search(/1/g); //1
'a,b,c,d'.split(',');//['a','b','c','d']
'a1b2c3d4'.split(/\d/);//['a','b','c','d']
如对本文有疑问, 点击进行留言回复!!
MFC的静态库.lib、动态库.dll(包含引入库.lib)以及Unicode库示例
CTF 刷题记录(一) 白云新闻搜索(手动与自动化SQL注入)
javascript如何使用函数random来实现课堂随机点名方法详解
网友评论