创建正则表达式对象
-
使用构造函数的方式
let reg1 = new RegExp(‘<body>’) // 方式1
let reg2 = new RegExp(/<body>/) // 方式2
-
使用字变量方式
let reg3 = /<body>/
正则表达式对象的test方法
语法:正则对象.test(“检测的字符串”)
返回值:布尔值,检测字符串是否满足正则规则,满足条件的话返回ture,否则返回false
let str = "edfc"; // 检测字符串
let reg1 = /a/; // 正则规则(匹配字符串是否存在字母a)
console.log(reg1.test(str)); // 返回结果:false,str字符串不存在字母 a
元字符/预定类
事先赋予含义的字符
- . (小数点):除了换行(\n)和回车(\r)之外的任意字符
- \d:数字字符
- \D:非数字字符
- \s:不可见字符(\t 制表符,\n 换行符,\r 回车符,\f 换页符,\v 垂直制表符)
- \S:可见字符。
- \w: 单词字符串(所有的字母数字和_下划线)
- \W:非单词字符(除了字母,数字,下划线的其它字符)
// 例 子
const res1 = /./.test("\r"); // 返回值:false。 \r 表示回车,不满足条件。
const res2 = /\d/.test("abcd4oo"); // 返回值:true。 只要存在一个数字就会返回true
const res3 = /\D/.test("abcdefg"); // 返回值:true。只要存在一个非数字就会返回true
const res4 = /\s/.test("eee"); // 返回值:false。不存在不可见字符,则返回false
const res5 = /\S/.test("\r111"); // 返回值:true,只要存在可见字符,就返回true。
范围类
[]:中括号只表示一个字符。
例如:
[abc]:只要存在 a 或 b 或 c就满足条件。
const reg1 = /[abc]/.test("a"); // true
const reg2 = /[abc]/.test("abc"); // true
const reg3 = /[abc]/.test("hello"); // false 字符串hello,不存在字符a、b、c。其中一个
[a-z]:字母 a b c d … z 中的26个小写字母。
[A-Z]:字母 A B C D … Z中的26个大小字母。
[0-9]:数字 0,1,2,3,4,5,6,7,8,9 中的10个数字。
# 以下字符串只要有一个字符满足条件就会返回true。
const reg1 = /[0-2]/.test("219"); // true 1 2 在 0-2范围内,所以返回 true
const reg2 = /[1-9]/.test("0"); // false
const reg3 = /[a-z]/.test("abc") // true
const reg4 = /[a-z]/.test("ABC") // false
负向类/取反
用法:[^]
注意:取反是中括号内的 ^ 符号。 [^]
// 这里意思是:检测的字符串,只要有含有 a b c 之外的字符就会返回true
/[^abc]/.test('a') // false
/[^abc]/.test('a1') // true
// [^sa]:表示不含有 s 或 a。
// 第一位是 a
// 第三位是 c
// 第二位是除了 s 或 a 之外的任意字符。
/a[^sa]c/.test("abc") // true
/a[^sa]c/.test("abdc") // false
边界
/^/:以什么字符开头,例如:/^abc/ 字符串以abc开头。
/$/:以什么字符结尾,例如:/cc$/ 字符串以 cc结尾。
注意:这里 取反( [^] ) 是不一样的含义。
- 取反是限定在中括号内 [] 的。
- 边界的写法: /^ /
/[^\d]/.test("1") // 以数字开头,这里返回 true
/^6a/.test("ff") // 以6a开头, 这里字符串为 ff, 结果为 false
/ac$/.test("ddac") // 这里以 ac 结尾,字符串 ddac 符合正则,结果为 true
严格匹配
写法:/^字符串$/ 匹配结果有且只有一个。
/^abc$/.test("abc") // true
/^abc$/.test("abcabcabc") // false
量词
- * :重复零次或更多次 x > = 0
const str = "abcdef";
// \d 表示数字 后面加 * 表示 有 0 个或者 多 个数字
// str 字符串中含有a,a后面有没有数字都满足条件。返回结果为:true
const reg = /a\d*/.test(str); // true
- + :重复一次或更多次 x >= 1
const str = "abcdef";
// \d 表示数字 后面加 + 表示 有 1 个或者 多 个数字
// str 字符串中虽然含有a,但是a后面没有数字。返回结果为:false
const reg = /a\d+/.test(str); // false
- ?:重复零次或一次 x = (0|1);
const str = "a2bcdef";
// 字符串 只要存在 a12 或 a2 都返回 true
const reg = /a1?2/.test(str); // true
-
{n}:出现 n 次,x = n
-
{n,}:至少重复n次,x>= n
-
{n, m}:重复n-m次之间, n<= x <=m
// a{2} 只要a连续出现2次就返回true
console.log(/a{2}/.test("aaef")); // true
// b{2,4} 字符串只要出现 bb 或 bbb 或 bbbb 就返回true
console.log(/b{2,4}/.test("bb")); // true
// c{2,} 字符串只要连续出现2个c或者以上的就返回true
console.log(/c{2,}/.test("cefc")); // false
| 、()
| 表示或,只要满足其中一个条件都返回 true
# 只要字符串中含有 黑 或 白 就返回true
console.log(/黑|白/.test("黑")); // true
console.log(/黑|白/.test("白")); // true
console.log(/黑|白/.test("黑白")); // true
console.log(/黑|白/.test("灰")); // false
()优先级、或表示一个整体
# /a(bc){2}d/ 这里表示 (bc) 是一个整体,后面的2修饰bc,表示bc要连续出现2次。 所以正则的结果为 abcbcd
console.log(/a(bc){2}d/.test("abcbcd")); // true
# /abc{2}d/ {2} 修饰的是前面的 c,c要连续出现2次,正则结果为 abccd。
console.log(/abc{2}d/.test("abcbcd")); // false
# (b|c) 这里表示可能是 b 或 c。{2}修饰第二位字符,所以结果可能是:abbd accd
console.log(/a(b|c){2}d/.test("abbd")); // true
replace
用于字符串替换
const str = "abcdbb";
const newStr = str.replace(/b/, "B"); // g 全局匹配
console.log(newStr); // 默认值替换第一个匹配到的值 aBcdbb
str.replace(/b/g, "B") // /b/g g表示全局匹配 这里替换字符串中所有的 b。
# 例子:把字符串中的数字统一 替换为 1000
let str = "大哥300, 二哥500,三哥900";
let newStr = str.replace(/\d+/g, 1000); // 大哥1000, 二哥1000,三哥1000
# 例子2:
let str = "5=a, 哈哈 6=b,呵呵 7=c";
// $1 表示第一个括号匹配的内容,$2 表示第二个括号匹配的内容,以此类推。
let newStr = str.replace(/(\d)=(\w)/g, "$2=$1");
console.log(newStr) // a=5, 哈哈 b=6,呵呵 c=7
match
用于提取字符串中,符合正则规则的值。
// 如果要提取的字符串中有符合正则规则的,就返回一个数组。
// 如果提取的字符串中没有符合正则规则的,就返回一个null。
let str = "大哥300二哥200三哥300";
// \d 表示 数字, + 表示 一个或多个。所以该正则提取三个数字。
let res = str.match(/\d+/g); // 结果: ['300', '200', '300']
search
字符串检索,和 indexOf 作用一样
let str1 = "hello world";
let res = str1.search(/wo/);
console.log(res, "--res --"); // 返回 索引 6
split
字符串分割,返回结果是数组。
let str = "hello world";
let res = str.split(" "); // 根据空格分割
console.log(res); // 返回结果: ['hello', 'world']
例子:split 传入正则表达式。
根据字符串中的数字进行分割
let str = "hello1world32school";
let res = str.split(/\d+/); // \d 表示 数字, + 表示 一个或多个。
console.log(res) // ['hello', 'world', 'school']
exec
正则对象的exec方法,提取满足正则规则的第一个字符 (匹配不到就返回null)
# 这里提取三个数字,222、999满足。这里值返回了 只获取到第一个匹配项
let res = /\d{3}/.exec("哈哈哈222呵呵呵呵999");
console.log(res) // ['222', index: 3, input: '哈哈哈222呵呵呵呵999', groups: undefined]