var str = "1a1b1c";
var reg = new RegExp("1(.)", "g");
alert(reg.test(str)); // true
alert(reg.test(str)); // true
/*alert(reg.test(str)); // trued
alert(reg.test(str)); // trued */
alert(RegExp.$1);
/*
var reg = new RegExp("1.", "g");
alert(reg.exec(str)[0]);
alert(reg.exec(str)[0]); */
格式: http://www.cnblogs.com/shi0090/p/5971554.html
var expression=/at/gi;
expression = new RegExp(pattern, flags); \[ 脱离转义符
正则转义符:( [ { ^ $ | ) ? * + . ] } 加入 [ 脱离转义符
.可变身
[ab]只能变成a,b
转义作用()
var text = "mom and dad and baby";
var pattern = /mom( and dad( and baby)?)?/gi;
var matches = pattern.exec(text);
console.log(matches.index); // 0
console.log(matches.input); // "mom and dad and baby"
console.log(matches[0]); // "mom and dad and baby"
console.log(matches[1]); // " and dad and baby"
console.log(matches[2]); // " and baby"
var text = "000-00-0000";
var pattern = /d{3}-d{2}-d{4}/;
if (pattern.test(text)){
console.log("The pattern was matched.");
}
var text = "this has been a xhort summer";
var pattern = /(.)hort/g;
if (pattern.test(text)){
console.log(RegExp.leftContext); // this has been a
console.log(RegExp.rightContext); // summer
console.log(RegExp.lastMatch); // short
console.log(RegExp.lastParen); // s
console.log(RegExp.$1);
console.log(RegExp.multiline); // false
}
var a = 'aaaa'.match(/w/g);
console.log(a); // ["a", "a", "a", "a"]
var a = 'aaaa'.match(/w/);
console.log(a); // ["a", index: 0, input: "aaaa"]
'aaaa'.replace('a', 'b') //"baaa"