1.检查邮政编码
1 var pattern=/[1-9][0-9]{5}/; // 必须是6位,必须是数字,第一位不能为0
2 var str='274200';
3 alert(pattern.test(str));
2.检查文件的压缩包
1 var pattern=/^[w]+.zip|gz|rar|7z/; // w+ ,加上^来限定从首字符开始匹配
2 var str='213.zip'; //文件名:字母 _ 数字 .zip .gz .rar .7z
3 alert(pattern.test(str));
3.删除多余空格
1 var pattern=/s/g;
2 var str='11 222 333';
3 alert(str.replace(pattern,''));
4.删除首尾空格
1 //一般方法
2 var pattern=/^s+/;
3 var str=' goo gle ';
4 var result=str.replace(pattern,'');
5 pattern=/s$/;
6 result=result.replace(pattern,'');
7 alert(result)
8
9 //使用非贪婪捕获 惰性模式
10 var pattern=/^s+(.+?)s+$/; //(.+?)是惰性模式
11 var str=' goo gle ';
12 var result=pattern.exec(str)[1];
13 alert('|'+result+'|')
14
15 //使用分组捕获
16 var pattern=/^s+(.+?)s+$/; //(.+?)是惰性模式
17 var str=' goo gle ';
18 var result=str.replace(pattern,'$1');
19 alert('|'+result+'|')
5.验证简单的电子邮件地址
1 var pattern=/^([w.-]+)@([w-]+).([a-zA-Z]{2,4}$)/;
2 var str='GuoXiangyue0721@163.com';
3 alert(pattern.test(str));