一、检查邮政编码
var pattern = /[1-9][0-9]{5}/; //共 6 位数字,第一位不能为0 {5}表示后面5位0到9 var str = '224000'; alert(pattern.test(str));
二、检查是否是文件压缩包
var pattern = /[w]+.zip|rar|gz/; //dw_表示所有数字和字母加下划线 .表示匹配.,后面是一个选择 var str = '123.zip'; alert(pattern.test(str));
三、简单的电子邮件验证
var pattern = /^([a-zA-Z0-9_.-]+)@([a-zA-Z0-9_.-]+).([a-zA-Z]{2,4})$/; var str = 'yc60.com@gmail.com'; alert(pattern.test(str));
/*删除多余空格*/ var pattern = /s/g; //g 必须全局,才能全部匹配 var str = '111 222 333'; var result = str.replace(pattern,''); //把空格匹配成无空格 alert(result); /**/ /*删除首尾空格*/ var pattern = /^s+/; //强制首 var str = ' goo gle '; var result = str.replace(pattern, ''); pattern = /s+$/; //强制尾 result = result.replace(pattern, ''); alert('|' + result + '|'); var pattern = /^s*(.+?)s*$/; //使用了非贪婪捕获 var str = ' google '; alert('|' + pattern.exec(str)[1] + '|'); var pattern = /^s*(.+?)s*$/; var str = ' google '; alert('|' + str.replace(pattern, '$1') + '|'); //使用了分组获取