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);html
/*
var reg = new RegExp("1.", "g");
alert(reg.exec(str)[0]);
alert(reg.exec(str)[0]); */express
格式: http://www.cnblogs.com/shi0090/p/5971554.html
var expression=/at/gi;
expression = new RegExp(pattern, flags); \\[ 脫離轉義符
正則轉義符:( [ { \ ^ $ | ) ? * + . ] } 加入 \[ 脫離轉義符this
.可變身
[ab]只能變成a,b
轉義做用()
var text = "mom and dad and baby";
var pattern = /mom( and dad( and baby)?)?/gi;htm
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"blog
var text = "000-00-0000";
var pattern = /\d{3}-\d{2}-\d{4}/;input
if (pattern.test(text)){
console.log("The pattern was matched.");
}io
var text = "this has been a xhort summer";
var pattern = /(.)hort/g;console
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
}ast
var a = 'aaaa'.match(/\w/g);
console.log(a); // ["a", "a", "a", "a"]test
var a = 'aaaa'.match(/\w/);
console.log(a); // ["a", index: 0, input: "aaaa"]
'aaaa'.replace('a', 'b') //"baaa"