概念
正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符(修饰符、元字符等)、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。
提示:同一验证、替换、提取规则,对应的正则表达式不一定一样,有多种写法。验证符合规则即可。
作用
1. 验证数据的有效性
2. 替换文本内容
3. 从字符串中提取子字符串
修饰符
修饰符 | 描述 |
---|---|
i | 不区分大小写 , 搜索过滤时A 和 a 没有区别。 |
g | 全局匹配,查找所有的匹配项。 |
使用在线工具 ,测试练习:
例: 已知字符串 “apple after BAN” ,匹配其中的字母A。
- 没有任何修饰符
- 不区分大小写
- 全局匹配
元字符
这里列举一些比较常用或使用频率比较高的元字符
字符 | 描述 |
---|---|
^ | 匹配字符串的开始位置 |
$ | 匹配字符串的结束位置 |
\d | 匹配一个数字字符。等价于 [0-9] |
\D | 匹配一个非数字字符。等价于 [^0-9]。 |
\w | 匹配字母、数字、下划线。等价于’[A-Za-z0-9_]’。 |
\W | 匹配非字母、数字、下划线。等价于 ‘[^A-Za-z0-9_]’。 |
* | 匹配前面的表达式0次或多次 。 如:bo* 能匹配b 、bo、boo…等等。 相当于:{0,} |
+ | 匹配前面的表达式一次或者多次。如:bo+ 能匹配bo、boo…等,但不能匹配b,相当于{1,} |
? | 匹配前面的表达式零次或者一次 。如:bo(ss)? 能匹配 bo、boXX…或者 boss、bossXXXX ,相当于{0,1} |
{n} | n是一个非负整数,匹配n次。如:p{2},能匹配 apple,但不能匹配 please |
{n,} | n是一个非负整数,至少匹配n次。如 :p{2,}能匹配 apple、appple、apppple…等。但不能匹配 aple |
{n,m} | n、m都是非负整数,n
≤
\leq
≤ m, 至少匹配n次且最多匹配m次。如:p{2,3},能匹配apple 、appple。 注:appppple 如果是修饰符是全局匹配能匹配到两次 ,ppp和pp |
x|y | 匹配x或y。如: x|yoo,匹配x 或 yoo;(x|y)oo匹配 xoo或yoo |
[xyz] | 字符集合。匹配所包含的任意一个字符。如: [abc]可以匹配 class中 c、a |
[^xyz] | 字符集合取反。匹配不在集合中的任意字符。如: [^abc] 可以匹配class中的 l 、s、s |
[a-z] | 字符范围,匹配指定范围内的任意字符。如:[a-z]可以匹配a-z范围内的任意小写字母字符 |
[^a-z] | 字符范围取反,匹配任何不在指定范围内的任意字符。如:[^a-z]可以匹配不在a-z范围内的小写字母外的任意字符 |
正则表达式的方法
方法 | 描述 |
---|---|
compile | 编译正则表达式。 |
exec | 检索字符串中指定的值。返回找到的值,并确定其位置。 |
test | 检索字符串是否匹配某种模式。返回 true 或 false。 |
例:
exec函数:
let str = "hello world";
let reg = /world/g;
let str2 = reg.exec(str);
console.log(`检索结果:${str2[0]}, 所在位置:${str2.index}`);
// 检索结果:world, 所在位置:6
test函数: 这里world能完全匹配到,所以为true,但没有0-9的字符,所以false
let str = "hello world";
let reg = /world/g;
let str1= reg.test(str);
console.log(`匹配结果:${str1}`);
//匹配结果:true
let reg1=/[0-9]/g;
console.log(reg1.test(str));
//false
支持正则表达式的 String 对象的方法
注:这里都是String的方法。
方法 | 描述 |
---|---|
search | 检索与正则表达式相匹配的值 |
match | 找到一个或多个正则表达式的匹配 |
replace | 替换与正则表达式匹配的子串。 |
split | 把字符串分割为字符串数组。 |
Demo演示
seach函数:search不执行全局匹配,它将忽略标志 g,所以返回第一个与之匹配的起始位置。没匹配返回-1
但是对大小写敏感。
let str = "js hello wo324rld HELLO hello";
let reg1 = /(he)?llo/;
console.log(`${str.search(reg1)} --- ${str.search(/(he)?llo/g)}`);
// 3 --- 3
//说明:有没有全局修饰符g,默认都返回第一个查找到的数据
let reg2 = /HELLO/i;
console.log(`忽略大小写: ${str.search(reg2)}`);
// 忽略大小写: 3
let reg3 = /[0-9]/;
console.log(str.search(reg3));
// 11
match() 方法:可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。返回匹配的值。注意使用修饰符g和不使用修饰符返回字符的区别。
let str = "js hello wo324rld HELLO hello";
console.log(str.match(/hello/));
// ["hello", index: 3, input: "js hello wo324rld HELLO hello", groups: undefined]
// 说明:不使用全局修饰符g,只能匹配第一个,含有index起始位置
console.log(str.match(/hello/g));
// ["hello", "hello"]
//说明:这里使用修饰符g,匹配结果中没有匹配的起始位置index,如果需要,建议使用exec函数,如下
let reg = /hello/g;
let result;
while (result = reg.exec(str)) {
console.log(result, result.index, reg.lastIndex);
}
// ["hello", index: 3, input: "js hello wo324rld HELLO hello", groups: undefined] 3 8
// ["hello", index: 24, input: "js hello wo324rld HELLO hello", groups: undefined] 24 29
console.log(str.match(/hello/ig));
// ["hello", "HELLO", "hello"]
replace 函数用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
let str = "js hello wo324rld HELLO hello";
console.log(str.replace(/hello/, "javascript"));
//js javascript wo324rld HELLO hello
//说明: 默认替换第一个匹配字符串
console.log(str.replace(/hello/g, "javascript"));
//js javascript wo324rld HELLO javascript
//全局匹配修饰符g
console.log(str.replace(/hello/ig, "javascript"));
//js javascript wo324rld javascript javascript
//全局匹配修饰符g 和 i
split 函数用于把一个字符串分割成字符串数组。
let str = "js hello wo324rld HELLO hello";
let result1 = str.split(/hello/);
console.log(JSON.stringify(result1));
//["js "," wo324rld HELLO ",""]
let result2 = str.split(/hello/i);
console.log(`忽略大小写: ${JSON.stringify(result2)}`);
//忽略大小写: ["js "," wo324rld "," ",""]
let result3 = str.split(/\d/);
console.log(`通过数字分割 :${JSON.stringify(result3)}`);
//通过数字分割 :["js hello wo","","","rld HELLO hello"]
let str1="js hello wo324rld HELLO hello _ &&9 ! _@ ";
let result4 = str1.split(/\w/);
console.log(JSON.stringify(result4));
//["",""," ","","","",""," ","","","","","","",""," ","","","",""," ","","","",""," "," &&"," ! ","@ "]
//说明:通过字母、数字、下划线分割
let result5 = "str hellto s& 6 !".split("");
console.log(`转化字符数组: ${JSON.stringify(result5)}`);
//转化字符数组:
// ["s","t","r"," ","h","e","l","l","t","o"," ","s","&"," ","6"," ","!"]
常用正则案例 (javascript字面量形式)
- 数字(注意:含0开头的数字) : ^[0-9]+$ 或 ^\d+$
- n位数字:^\d{n}$
- 至少n位数字:^\d{n,}$
- n -m 位数字:^\d{n,m}$
- 零和非零开头的数字:^(0|[1-9][0-9]*)$
- 正整数 : ^[1-9]\d*$ 或 **^[1-9][0-9]*$
- 汉字:^[\u4e00-\u9fa5]+$
- 英文字母:^[A-Za-z]+$
- 由数字和26个英文字母组成的字符串:^[A-Za-z0-9]+$