var str = "cat3 hat4"; var re = /\w+\d/; var ex = re.exec(str); var mt = str.match(re); console.log(ex); console.log(mt);
var str = "cat3 hat4"; var re = /\w+\d/g; var ex = re.exec(str); var mt = str.match(re); console.log(ex); console.log(mt);
var str = "cat3 hat4"; var re = /\w+(\d)/; var ex = re.exec(str); var mt = str.match(re); console.log(ex); console.log(mt);都是["cat3", "3"] 也就是说如果带分组但是有不带g 这2个方法结果是一样的..第一个元素是匹配的 第2个开始就是分组1,,,,
var str = "cat3 hat4"; var re = /\w+(\d)/g; var ex = re.exec(str); var mt = str.match(re); console.log(ex); console.log(mt);
还有他们2个exec是正则对象的方法 而match是字符串的方法
原文:http://blog.csdn.net/atvance016/article/details/40840113