js正则表达式

修饰符
修饰符描述
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值