/* //复习 var obj= new Number(2); alert(Number.MIN_VALUE); var num = new Number(22e3); alert(num); alert(isFinite(num));//判断是否超出了规定的范围 var person={fname:"John",lname:"Doe",age:25}; var txt=""; for (x in person) { txt=txt + person[x]; } alert(txt); ------------------------------------------ 正则表达式(regular expression) 创建方式: 1.使用new运算符 var reg = new RegExp("box"); alert(reg);//return /box/ var reg = new RegExp("box","ig");//模式修饰符的可选参数 : i-忽略大小写;g-全局匹配;m-多行匹配。 alert(reg);//return /box/gi 2.使用字面量方式创建 var reg = /box/; alert(reg);//return /box/ var reg = /box/ig; alert(reg);//return /box/gi --------------------------- var pattern = new RegExp("Box"); var str = new String("box"); alert(pattern.test(str));//return false var pattern = new RegExp("Box","i"); var str = new String("This is a box"); alert(pattern.test(str));//return true alert(/Box/.test("This is a box!"));//false alert(/Box/i.test("This is a box!"));//true alert(/Box/.exec("This is a box!"));//return null alert(/Box/i.exec("This is a box!"));//return box alert(/Box/i.exec("This box is a Box!"));//return box alert(/Box/i.exec("This Box is a box!")); //return Box alert(/Box/ig.exec("This box is a Box!"));//return box var pattern = new RegExp("Box","i"); var str = new String("This is a Box! That box is a Box!"); var result = pattern.exec(str); var result2 = str.match(pattern);//String对象的方法 alert(typeof result2); alert(result2[0]); var pattern = new RegExp("Box","ig");//全局匹配,即找到所有匹配项目 var str = new String("This box is a Box! That box is a Box!"); var result = pattern.exec(str);//返回第一个匹配的字符串 alert(result[1]);//return undefined 证明返回的不是数组 var result2 = str.match(pattern);//String对象的方法 alert(typeof result2); alert(result2[1]);//return box证明返回的是一个数组 var pattern = new RegExp("Box","i"); var str = new String("This box is a Box! That box is a Box!"); var result = str.search(pattern);//String对象的方法 alert(result);//return 5 返回第一个匹配的位置,找不到就返回-1 var pattern = new RegExp("Box","i");//没有开启全局 var str = new String("This box is a Box! That box is a Box!"); var result = str.replace(pattern,"Tom"); alert(result);//This Tom is a Box! That box is a Box! var pattern = new RegExp("Box","ig");//开启全局 var str = new String("This box is a Box! That box is a Box!"); var result = str.replace(pattern,"Tom"); alert(result);//This Tom is a Tom! That Tom is a Tom! var pattern = new RegExp("!","ig");//开启全局 var str = new String("This box is a Box! That box is a Box!"); var result = str.split(pattern); alert(result);//This box is a Box, That box is a Box, var pattern = new RegExp("!","ig");//开启全局 var str = new String("This box is a Box! That box is a Box!"); var result = str.split(pattern); for(var i=0;i<result.length;i++){ alert(result[i]); } var pattern = new RegExp("!","ig");//开启全局 var str = new String("This box is a Box! That box is a Box!"); var result = str.split(pattern); for(rs in result){ alert(rs);//return 0, return 1, return 2 } var pattern = new RegExp("Box","ig");//开启全局 var str = new String("This box is a Box! That box is a Box!"); var result = pattern.test(str); //alert(RegExp.input);//This box is a Box! That box is a Box! alert(RegExp.leftContext); alert(RegExp.rightContext); alert(RegExp.lastMatch); var pattern = /g..gle/; //.表示除了换行符以外的其他任意字符 var str = "google"; alert(pattern.test(str));//true var pattern = /go*gle/; //o*表示0到多个o var str = "gooogle"; alert(pattern.test(str));//true var pattern = /go+gle/; //o+表示1到多个o var str = "gooogle"; alert(pattern.test(str));//true var pattern = /go?gle/; //o?表示0个,或者1个 var str = "ggle"; alert(pattern.test(str));//true var pattern = /go{2,4}gle/; //o{2,4}表示2~4个o,包含2和4 var str = "google"; alert(pattern.test(str));//true var pattern = /go{2}gle/; //o{2}表示只出现2个o var str = "google"; alert(pattern.test(str));//true var pattern = /go{2,}gle/; //o{2,}表示o出现2个或者2个以上 var str = "gooogle"; alert(pattern.test(str));//true var pattern = /go[a-z]gle/; //[a-z]表示26个小写字母中的任意一个 var str = "google"; alert(pattern.test(str));//true var pattern = /go[0-9]gle/; //[0-9]表示0~9中的任意一个 var str = "go8gle"; alert(pattern.test(str));//true var pattern = /go[a-zA-Z0-9]gle/; //[a-zA-Z0-9]是一种复合写法,表示26个大小写字母或数字中的任意一个 var str = "go8gle"; alert(pattern.test(str));//true var pattern = /go[^0-9]gle/; //[^0-9]表示非0~9中的任意一个字符 var str = "go8gle"; alert(pattern.test(str));//false var pattern = /^go[0-9]gle/; //^表示从头开始匹配 var str = "ggo8gle"; alert(pattern.test(str));//false var pattern = /go\wgle/; //\w表示任意一个字母,数字或下划线,等同于[a-zA-Z0-9_] var str = "go_gle"; alert(pattern.test(str));//true var pattern = /go\Wgle/; //\w表示任意一个非字母,非数字且非下划线的字符,等同于[^a-zA-Z0-9_] var str = "go_gle"; alert(pattern.test(str));//false var pattern = /go\dgle/; //\d表示任意一个数字,等同于[0-9] var str = "go9gle"; alert(pattern.test(str));//true var pattern = /go\Dgle/; //\D表示任意一个非数字的字符,等同于[^0-9] var str = "go9gle"; alert(pattern.test(str));//false var pattern = /google$/; //$表示从行尾开始匹配 var str = "ggoogle"; alert(pattern.test(str));//true var pattern = /goo\sgle/; //\s查找空白字符。 var str = "ggoo gle"; alert(pattern.test(str));//true var pattern = /google\b/; //\b匹配单词边界。类似于$ var str = "ggoogle"; alert(pattern.test(str));//true var pattern = /google|baidu|bing/; //竖线|表示匹配或选择模式 var str = "ggoogle"; alert(pattern.test(str));//true var pattern = /(google|baidu|bing)s/; //竖线|表示匹配或选择模式 var str = "ggoogles"; alert(pattern.test(str));//true var pattern = /google{4,8}$/; //表示结尾处e出现4~8次 var str = "ggoogleeee"; alert(pattern.test(str));//true var pattern = /(google){4,8}$/; //括号表示分组,表示结尾处google出现4~8次 var str = "ggooglegooglegooglegoogle"; alert(pattern.test(str));//true var pattern = /8(.*)8/; //分组 var str = "goo8google8gle"; //pattern.test(str); //pattern.exec(str); str.match(pattern); alert(RegExp.$1);//google RegExp.$1表示获取模式中第一个分组对应的匹配字符串 var pattern = /8(.*)8/; //分组 var str = "this is 8google8"; document.write(str.replace(pattern,"<strong>google</strong>")); var pattern = /8(.*)8/; //分组 var str = "this is 8google8"; document.write(str.replace(pattern,"<strong>$1</strong>")); var pattern = /8(.*)8/; //分组 var str = "this is 8google8"; document.write(str.replace(pattern,"<strong>$1</strong>")); var pattern = /[a-z]/; var str = "you love"; alert(str.replace(pattern,"1"));//1ou love var pattern = /[a-z]+/; //+贪婪模式 var str = "you love"; alert(str.replace(pattern,"1"));//1 love var pattern = /[a-z]+?/; //使用+?惰性模式 var str = "you love"; alert(str.replace(pattern,"1"));//1ou love var pattern = /8(.*)8/; //使用了贪婪模式 var str = "this is 8google8 8google8 8google8 "; document.write(str.replace(pattern,"<strong>$1</strong>"));//this is <strong>google8 8google8 8google</strong> var pattern = /8(.*?)8/; //使用了惰性模式 var str = "this is 8google8 8google8 8google8 "; document.write(str.replace(pattern,"<strong>$1</strong>"));//this is <strong>google</strong> 8google8 8google8 var pattern = /8(.*?)8/g; //使用了惰性模式,开启全局 var str = "this is 8google8 8google8 8google8 "; document.write(str.replace(pattern,"<strong>$1</strong>"));//this is <strong>google</strong><strong>google</strong><strong>google</strong> var pattern = /8([^8]*)8/g; //另一种惰性,屏蔽了8的匹配,开启全局 var str = "this is 8google8 8google8 8google8 "; document.write(str.replace(pattern,"<strong>$1</strong>"));//this is <strong>google</strong><strong>google</strong><strong>google</strong> var pattern = /8([^8]*)8/g; //另一种惰性,屏蔽了8的匹配,开启全局 var str = "this is 8google8 8google8 8google8 "; document.write(str.replace(pattern,"<strong>$1</strong>"));//this is <strong>google</strong><strong>google</strong><strong>google</strong> var pattern=/(\d+)([a-z])/; //捕获型分组,所有的分组都捕获返回 var str="123abc"; var a=pattern.exec(str); alert(a[0]);//123a 返回匹配到的字符串 alert(a[1]);//123 返回第一个分组匹配到的字符串 alert(a[2]);//a 返回第二个分组匹配到的字符串 var pattern=/(\d+)(?:[a-z])/; //非捕获型分组,只需在不需要捕获返回的分组加上?: var str="123abc"; var a=pattern.exec(str); alert(a[0]);//123a 返回匹配到的字符串 alert(a[1]);//123 返回第一个分组匹配到的字符串 alert(a[2]);//undefined 返回第二个分组匹配到的字符串 console.log("test zhangweiwen"); var pattern=/(a?(b?(c?)))/;//嵌套匹配,从外到内 var str="abc"; alert(pattern.exec(str));//abc,abc,bc,c var pattern=/goo(?=gle)/;//必须是后面跟着gle,才能返回goo var str="google"; alert(pattern.exec(str));//goo var pattern=/[1-9][0-9]{5}/; var str="741027";//必须是6位,必须为数字,首位不能为0 alert(pattern.test(str)); alert(str.match(pattern)); var pattern=/^\w*\.zip|7z&/; //字母,数字,下划线为压缩文件的名 var str="study_2015.zip"; alert(pattern.test(str)); var pattern=/(\s+)/g; var str=" goog le "; alert(str.replace(pattern,",")); //alert("|"+pattern.exec(str)+"|"); alert(str.match(pattern)); var pattern=/(\w+)/g; var str=" goog le a"; alert(str.replace(pattern,",")); //alert("|"+pattern.exec(str)+"|"); //|goog,goog| 第一个表示返回的所有分组,第二个goog表示返回的第一个分组匹配到的字符串 alert(str.match(pattern)); //goog,le,a 返回所有匹配到的字符串 var pattern=/^\s+(.+?)\s+$/; // /.+/表示贪婪模式 /.+?/表示惰性模式 var str=" goog le "; alert("|"+str.replace(pattern,"$1")+"|"); //将匹配的结果用第一个分组的匹配结果字符串替换 alert(RegExp.$1); //可以查看第一个分组的匹配结果 */