JavaScript 正则表达式
/i 不区分大小写/
/g 全局匹配而不是匹配到了就返回/
/m 换行匹配/
分组
圆括号的用处
1、便于使用 | * + 等贪婪字符
2、用于允许在同一正则表达式的后部引用前面的子表达式
一、
function suportWxPay(){
var userAgent = "micromessenger/";
var reg = /micromessenger\/([\d\.])/i;
var r = userAgent.match(reg);
console.log(r);
};
suportWxPay();
null
二、
function suportWxPay(){
var userAgent = "micromessenger/";
var reg = /micromessenger\/([\d\.])/ig;
var r = userAgent.match(reg);
console.log(r);
};
suportWxPay();
null
三、
function suportWxPay(){
var userAgent = "micromessenger/";
var reg = /micromessenger\/([\d\.]*)/i;
var r = userAgent.match(reg);
console.log(r);
};
suportWxPay();
Array [ "micromessenger/", "" ]
四、
function suportWxPay(){
var userAgent = "micromessenger/";
var reg = /micromessenger\/([\d\.]*)/ig;
var r = userAgent.match(reg);
console.log(r);
};
suportWxPay();
Array [ "micromessenger/" ]
五、
function suportWxPay(){
var userAgent = "micromessenger/523.234";
var reg = /micromessenger\/([\d\.]*)/ig;
var r = userAgent.match(reg);
console.log(r);
};
suportWxPay();
Array [ "micromessenger/523.234" ]
六、
function suportWxPay(){
var userAgent = "micromessenger/523.234";
var reg = /micromessenger\/([\d\.]*)/i;
var r = userAgent.match(reg);
console.log(r);
};
suportWxPay();
Array [ "micromessenger/523.234", "523.234" ]
结论:
1、 g 的标志用处
2、() 的用处