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)
}