正则表达式exec方法

 RegExp.prototype.exec方法
            语法:正则表达式名.exec(要检测的字符串)
            参数:要匹配的字符串 
            返回值:数组或者null 匹配到返回一个数组 没匹配到返回null

代码模块展示:

 let str = 'hello world javascript hello hello hello'
        // 使用构造函数创建正则表达式
        let reg = new RegExp('hello', 'img')
        // 使用直接字面量创建正则表达式
        let reg1 = /java/img

        // 正则表达式实例方法 RegExp.prototype.exec
        // 参数:要匹配的字符串 
        // 返回值:数组或者null 匹配到返回一个数组 没匹配到返回null
        console.log(reg.exec(str)) //['hello', index: 0, input: 'hello', groups: undefined]

        // 若想要全局匹配字符
        // 若想要把改字符串中的hello全部找出来
        while(true) {
            let res = reg.exec(str)
            if (!res) {
                break
            }
            console.log(res)
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值