正则表达式中的或
|
符号,在正则表达式对象中表示或
的意思。前面讲过单独一个字符在正则表达式中都会占据一个位置
1
2
3
4
5
6
|
<script>
var res;
res = "abcd".match(/abcd/g);
console.log(res);
</script>
|
案例源码

上面代码的匹配项是完整的abcd
,如果在b
和c
之前添加一个|
,意思就不同了,表示判断字符串中是否包含字符ab
或者字符cd
:
1
2
3
4
5
|
<script>
var res;
res = "acd abd abd".match(/ab|cd/g);
console.log(res);
</script>
|
案例源码

所以|
通常也会会放在()
中使用,比如:
1
2
3
4
5
6
7
|
<script>
var res;
// 判断字符串中是是否有字符ab,且后面紧跟着一个d或者e字符:
res = "abc abd abe".match(/ab(d|e)/g);
console.log(res);
</script>
|
案例源码
