字面量创建正则表达式
let hd = "hhhhuuu.com";
let a = "u";
console.log(/u/.test(hd));// true
console.log(/a/.test(hd));// false 不支持变量
console.log(eval(`/${a}/`).test(hd));// true
使用对象创建正则表达式
let hd = "hhhhuuu.com";
let reg = new RegExp("u","g");
console.log(reg.test(hd));
let a = "@";
let reg = new RegExp(a,"g"); //可以用变量
console.log(reg.test(hd));
"abc".replace(/\w/g, search => {
return '你';
}); //你你你
选择符的使用
let hd = "nihaoya";
console.log(/a|@/.test(hd)); // “|” 或者
原子表与中的选择符
| 字符 | 描述 |
|---|---|
| 原子表 [ABC] | 匹配 [...] 中的所有字符,例如 [aeiou] 匹配字符串 "google runoob taobao" 中所有的 e o u a 字母,字符是“或”的关系。 |
| 原子组 () | 标记一个子表达式的开始和结束位置。子表达式可以获取供以后使用。要匹配这些字符,请使用 ( 和 )。 |
// [] 实例
let reg = /[aeiou]/g;
let hd = "google runoob taobao";
console.log(hd.match(reg)); // ['o', 'o', 'e', 'u', 'o', 'o', 'a', 'o', 'a', 'o']
// () 实例
let reg = /(ae|ob)/; //"ae" 和 "oo" 分别是两个整体
let hd = "google runoob taobao";
console.log(hd.match(reg)); //['ob', 'ob', index: 11, input: 'google runoob taobao', groups: undefined]
转义需要好好理解
// . 除换行外的任何字符
// . 普通点,需要转义
let price = 23.34;
console.log(/\d+\.\d+/.test(price)); //字面量转义用 \
let reg = new RegExp("\\d+\\.\\d+");
console.log("\d" === "d");// true
console.log(reg.test(price)); // true
let url = "https://www.baidu.com";
console.log(/https?:\/\/\w+\.\w+\.\w+/.test(url));
| 字符 | 描述 |
|---|---|
| $ | 匹配输入[字符串]的结尾位置。如果设置了 RegExp 对象的 Multiline 属性,则 也匹配 ‘\n' 或 ‘\r'。要匹配 字符本身,请使用 \$。 |
| ( ) | 标记一个子表达式的开始和结束位置。子表达式可以获取供以后使用。要匹配这些字符,请使用\( 和 \) |
| * | 匹配前面的子表达式零次或多次。要匹配 * 字符,请使用 \* |
| + | 匹配前面的子表达式一次或多次。要匹配 + 字符,请使用 \+ |
| . | 匹配除换行符 \n之外的任何单字符。要匹配 .,请使用 \. |
| [ ] | 标记一个中括号表达式的开始。要匹配 [,请使用 \[ 和 \] |
| ? | 匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。要匹配 ? 字符,请使用 \? |
| \ | 将下一个字符标记为或特殊字符、或原义字符、或向后引用、或八进制转义符。要匹配 \ 字符,请使用 \ |
| / | 要匹配 / 字符,请使用 \/ |
| 匹配输入字符串的开始位置,除非在方括号表达式中使用,此时它表示不接受该字符集合。要匹配 ^ 字符本身,请使用 \^ | |
| { } | 标记限定符表达式的开始。要匹配 {,请使用 \{ 和 \} |
字符边界约束
| 字符 | 描述 |
|---|---|
| ^ | 匹配输入字符串的开始位置。如果设置了RegExp对象的Multiline属性,^也匹配“\n”或“\r”之后的位置。 |
| $ | 匹配输入字符串的结束位置。如果设置了RegExp对象的Multiline属性,$也匹配“\n”或“\r”之前的位置。 |
let hd = "3dddd";
console.log(/^\d$/.test(hd)); //false
console.log(/^\d+\w+$/.test(hd)); //true
数值与空白元字符
let hd = "nihaoya2022";
console.log(hd.match(/\d+/g));
| 字符 | 描述 |
|---|---|
| \d | 匹配一个数字字符。等价于[0-9]。 |
| \D | 匹配一个非数字字符。等价于[^0-9]。 |
let hd = `
张三:010-99999999,
李四:020-88888888
`;
console.log(hd.match(/\d{3}-\d{7,8}/g));
w W .元字符
| 字符 | 描述 | |
|---|---|---|
| \w | 匹配包括下划线的任何单词字符(字母数字下划线),等价于“[A-Za-z0-9_]”。 | |
| \W | 匹配任何非单词字符,等价于“[^A-Za-z0-9_]”。 | |
| . | 匹配除“\n”之外的任何单个字符。要匹配包括“\n”在内的任何字符,请使用像“`(. | \n)`”的模式。 |
如何精巧的匹配所有字符
[\s\S] [\d\D]
i 与 g 模式修正符
let hd = 'nihaoyaHH';
console.log(hd.match(/u/g));
console.log(hd.match(/u/i));
console.log(hd.match(/u/ig));// i 和 g 顺序没关系
| 修饰符 | 含义 | 描述 |
|---|---|---|
| i | ignore - 不区分大小写 | 将匹配设置为不区分大小写,搜索时不区分大小写: A 和 a 没有区别。 |
| g | global - 全局匹配 | 查找所有的匹配项。 |
| m | multi line - 多行匹配 | 使边界字符 ^ 和 $ 匹配每一行的开头和结尾,记住是多行,而不是整个字符串的开头和结尾。 |
| s | 特殊字符圆点 . 中包含换行符 \n | 默认情况下的圆点 . 是匹配除换行符 \n 之外的任何字符,加上 s 修饰符之后, . 中包含换行符 \n。 |
let hd = `
#1 js,200元 #
#2 php,300元 #
#9 nihaoya # hh
#3 node.js,180元 #
`;
let dd = hd.match(/^\s#\d+\s+.+\s+#$/gm).map(v => {
v = v.replace(/\s*#\d+\s*/, '').replace(/#/, '');
[name, price] = v.split(",");
return {name, price};
})
汉字与字符属性
let hd = "www.baidu.com 哈哈 加油~!";
console.log(hd.match(/\p{L}/gu));
u应该是 unicode的意思,猜想
console.log(hd.match(/\p{P}/gu));
P: 标点字符;
L:字母;
M:标记符号(一般不会单独出现);
Z:分隔符(比如空格、换行等);
S:符号(比如数学符号、货币符号等);
N:数字(比如阿拉伯数字、罗马数字等);
C:其他字符
| 元字符 | 含义 |
|---|---|
| \p{L} | 所有字母 |
| \p{N} | 所有数字,类似于 \d |
| [\p{N}\p{L}] | 所有数字和所有字母,类似于 \w 4 |
| \P{L} | 不是字母,等价于 [^\p{L}] |
| \P{N} | 不是数字,等价于 [^\p{N}] |
使用方式:
var reg1 = /\p{L}/u;
var reg2 = new RegExp(/\p{L}\p{Z}\p{N}/, 'u');
更多字符属性参考链接: www.jianshu.com/p/e9b7135e4…
汉字
let hd = `张三:010-99999999,李四:020-88888888`;
let res = hd.match(/\p{sc=Han}+/gu);//匹配中文
console.log(res);
使用 u 模式可以正确处理四个字符的 UTF-16 字节编码
let str = "𝒳𝒴";
console.table(str.match(/[𝒳𝒴]/)); //结果为乱字符"�"
console.table(str.match(/[𝒳𝒴]/u)); //结果正确 "𝒳"
lastIndex属性的作用
RegExp对象lastIndex 属性可以返回或者设置正则表达式开始匹配的位置
- 必须结合
g修饰符使用 - 对
exec方法有效 - 匹配完成时,
lastIndex会被重置为0
let hd = 'abcdefg';
let reg = /\w/g;
console.log(reg.lastIndex)
console.log(reg.exec(hd))
console.log(reg.lastIndex)
console.log(reg.exec(hd))
有效率的y模式
原子组引用完成替换操作
let hd = `
<h1>houdunren</h1>
<span>后盾人</span>
<h2>skien</h2>
`;
let reg = /<(h[1-6])>([\s\S]+)<\/\1>/gi;
hd.replace(reg, (p0, p1, p2)=> {
console.log('p0', p0)
console.log('p1', p1)
console.log('p2', p2)
});