1. pattern.test(str);
返回值是boolean类型,匹配返回true,不匹配返回false
2. pattern.exec(str);
返回值是object类型,匹配返回str对应的字符串,不匹配返回null
var pattern = new RegExp("box","igm");
var str = "Box";
alert(pattern.test(str)); // true
var pattern = /box/i;
var str = "This is a Box";
alert(pattern.test(str)); // true
var pattern = /box/i;
var str = "Box";
alert(pattern.exec(str)); //Box
var pattern = /box/i;
var str = "aox";
alert(pattern.exec(str)); //null
网友评论