修饰符
修饰符 | 描述 |
---|---|
i | 执行对大小写不敏感的匹配。 |
g | 执行全局匹配(查找所有匹配)。 |
m | 执行多行匹配。 |
元字符
元字符 | 描述 |
---|---|
. | 匹配除了换行符以外的任意字符 |
\s | 代表任意空白符(换行,制表,空格) |
\S | 匹配任意非空字符串 |
\b | 匹配单词边界,匹配单词的开头和结尾 |
\B | 匹配一个非单词边界 |
\d | 匹配一个数字[0-9] |
\D | 匹配一个数字,等价于[ ^ 0-9] |
\w | 匹配一个单字字符 |
\W | 匹配一个非单字字符 |
量词
量词 | 描述 |
---|---|
^n | 匹配字符串的开始,用[]括号里面表示排除 |
n$ | 匹配字符串的结束 |
n+ | 匹配任何包含至少一个 n 的字符串。 |
n* | 匹配任何包含零个或多个n的字符串 |
n? | 匹配任何包含零个或一个n的字符串 |
支持正则表达式的 String 对象的方法
方法 | 描述 |
---|---|
search | 检索与正则表达式相匹配的值,返回字符串中开始位置 |
match | 找到一个或多个正则表达式的匹配。 |
replace | 替换与正则表达式匹配的子串。 |
split | 把字符串分割为字符串数组。 |
RegExp 对象方法
方法 | 描述 |
---|---|
compile | 编译正则表达式。 |
exec | 检索字符串中指定的值。返回找到的值,并确定其位置。 |
test | 检索字符串中指定的值。返回 true 或 false。 |
例子
test
test检测句子中是否能匹配到字符
const test = new RegExp('hello world', 'ig');
console.log(test.test('hello world')); // true
exec
exec 返回的是数组,有就返回数组的值,没有返回为null
const test1 = new RegExp('hello world', 'ig');
console.log(test1.exec(' hello world')); // null
match
match(pattern) 将所有匹配的字符串组合成数组返回
const pattern=/Box/ig;
const str="This is a Box! The is a box!";
console.log(str.match(pattern));
search
search(pattern) 返回字符串中pattern开始位置,忽略全局匹配
const pattern1=/Box/i;
const str1="This is a Box! The is a box!";
console.log(str1.search(pattern1)); // 10
replace
replace(pattern) 替换匹配到的字符串
const pattern2=/Box/ig;
const str2="This is a Box! The is a box!";
console.log(str2.replace(pattern2,'Tom'));
split
split(pattern) 返回字符串指定pattern拆分数组
const pattern3 = / /ig; //空格
const str3 = "This is a Box! The is a box!";
console.log(str3.split(pattern3)); //以空格进行分割,返回的是数组
// 输出结果
// [ 'This', 'is', 'a', 'Box!', 'The', 'is', 'a', 'box!' ]
常用案例
// 匹配模式
// \w表示a-zA-Z_ 锚元字符匹配(^ $) ^强制收匹配 $强制尾匹配,并且只匹配一个
const pattern4=/^\woogle\d$/;
// const pattern4=/^[a-z]oogle\d$/;
const str4="aoogle2";
console.log(pattern4.test(str4)); // true
// 注意: ^符号在[]里面表示 非 在外边表示强制首匹配,并且只匹配一个 要想匹配多个值,使用+
// \b表示到达边界 , |表示匹配或选择模式
const pattern5=/baidu|google|bing/; //匹配或选择其中某个字符,不是相等,包含的意思
const str5 = "baidu a google";
console.log(pattern5.test(str5)); //返回true
// 检查邮政编码
const pattern6 = /^[1-9][0-9]{5}$/;
const str6 = "122534"; //共6位数,第一位不能为0
console.log(pattern6.test(str6)); // true
// 压缩包后缀名 \w等于a-zA-Z0-9_ 使用^限定从首字母匹配 .是特殊符号需要\n进行转义
// |选择符必须使用()进行分组
const pattern7 = /^[\w]+\.(zip|gz|rar)$/;
const str7="a12_.zip"; //文件名 字母_数字.zip,gz,rar
console.log(pattern7.test(str7)); // true
//删除多余空格
//方法一: 使用replace只匹配一个,所以使用+匹配多个
//匹配开头一个
var pattern8=/^\s+/;
var str8=" google ";
var result=str8.replace(pattern8,'');
//匹配结尾一个
pattern8=/\s+$/;
result=result.replace(pattern8,'');
console.log('|'+result+'|'); // |google|
//方法二: (.+)贪婪模式,使用惰性模式,后面的空格不让匹配
var pattern9=/^\s+(.+?)\s+$/;
var str9=" google ";
console.log('9');
var result=pattern9.exec(str9,'')[1];
console.log(result)
console.log('|'+result+'|');
//方法三: (.+)贪婪模式,改为惰性模式,使用分组模式,只取匹配的内容
var pattern11=/^\s+(.+?)\s+$/;
var str11=" google ";
var result=str11.replace(pattern11,'$1'); //使用分组模式
console.log(str11.match(pattern11));
console.log('|'+result+'|'); // |google|
//简单邮箱验证
var pattern22=/^([\w\.\_]+)@([\w\_]+)\.([a-zA-Z]){2,4}$/;
var str22="qzfweb@gmail.com";
console.log(pattern22.test(str)); // true