test和match的用法和区别

test()函数是RegExp对象的方法,参数是字符串,返回值是boolean类型。

match()函数是String对象的方法,参数是正则表达式,返回值是数组

test()用法:

var str='abcdefgabcdefg'
var reg=/bcd/g
console.log(reg.test(str)) //true

match()用法:

var str='abcdefgabcdefg'
var reg=/bcd/g
console.log(str.match(reg)) // ["bcd", "bcd"]

猜你喜欢

转载自blog.csdn.net/weixin_44594219/article/details/127071859