转载请注明出处:/iwanghang
觉得博文有用,请点赞,请评论,请关注,谢谢!~
index1-正则表达式:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>index1-正则表达式</title><script type="text/javascript">var qq = "1234567890123";// 验证QQ号码是否合法var reg = new RegExp(/^[1-9][0-9]{4,12}$/); // 首位数字1-9,其他位数字0-9,长度4-13alert(reg.test(qq)); // 打印结果:true 或者 false</script></head><body></body></html>
index2-RegExp-test-exec:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>index2-RegExp-test-exec</title><script type="text/javascript">var str = "hello world";// 验证字符串是否含有hellovar reg2 = new RegExp("hello");alert(reg2.test(str)); // 打印结果:true 或者 false// 验证字符串是否含有hellovar reg3 = /hello/;alert(reg3.test(str)); // 打印结果:true 或者 false// test 和 exec 的区别alert(reg2.exec(str)); // 打印结果:hello 或者 nullvar str2 = "Hello world"; // reg2,reg3,对于大小写有严格要求,所以使用 test 和 exec,返回结果为 false 或者 nullalert(reg2.exec(str2)); // 打印结果:hello 或者 null/**i 执行对大小写不敏感的匹配。g 执行全局匹配**/var reg4 = /hello/i; // 加i,表示不区分大小写alert(reg4.exec(str2)); // 打印结果:Hellovar reg5 = new RegExp("hello", "i"); // 同上alert(reg5.exec(str2)); // 打印结果:Hello</script></head><body></body></html>
index3-String:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>index3-String</title><script type="text/javascript">/*match() 检索(查找)*/var reg = /hello/i;var reg2 = /hello/ig; // ig全局匹配var str = "Hello World ~ Hello Future";var str2 = "Hello World ~ Hello Future";alert(str.match(reg)); // 打印结果:Helloalert(str.match(reg2)); // 打印结果:Hello,Hello/*search() 查找位置*/alert(str.search(reg)); // 打印结果:0alert(str.search(reg2)); // 打印结果:0/*replace() 替换*/alert(str.replace(reg, "hi`")); // 打印结果:hi` World ~ Hello Futurealert(str2.replace(reg2, "hi`")); // 打印结果:hi` World ~ hi` Future/*split() 分割*/alert(str.split(reg).length); // 打印结果:3alert(str.split(reg)); // 打印结果(3个元素):, World ~ , Futurealert(str.split("~")); // 打印结果(2个元素):Hello World , Hello Future</script></head><body></body></html>
index4-字符集:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>index4-字符集</title><script type="text/javascript">// . 代表任意字符,换行除外var reg = /b..k/;var str = "books";alert(reg.test(str)); // 打印结果:true// ? 出现0次或者1次// * 出现0次或者多次// + 出现1次或者多次var reg2 = /bo*k/;var str2 = "books";alert(reg2.test(str2)); // 打印结果:true// {m,n} 至少出现m次,最多出现n次var reg3 = /bo{2,6}k/;var str3 = "booooooks";alert(reg3.test(str3)); // 打印结果:true// [] 限定某一位上字符的取值范围var reg4 = /[a-z]ook/;var str4 = "books";alert(reg4.test(str4)); // 打印结果:truevar reg5 = /[a-zA-Z0-9]ook/;var str5 = "!ooks";alert(reg5.test(str5)); // 打印结果:false// [^] 限定某一位上字符不能取的值var reg6 = /[^0-9]ook/;var str6 = "5ooks";alert(reg6.test(str6)); // 打印结果:false/*等价值:\d 等价于[0-9]\D 等价于[^0-9]\w 等价于[a-zA-Z0-9_]\W 等价于[^a-zA-Z0-9_]*/var reg7 = /book/;var str7 = "wer book wer";alert(reg7.test(str7)); // 打印结果:truevar reg8 = /^book/; // 限定book开头 /^/匹配行首var str8 = "wer book wer";var str8_2 = "book wer";alert(reg8.test(str8)); // 打印结果:falsealert(reg8.test(str8_2)); // 打印结果:truevar reg9 = /^book$/; // 限定book开头和结尾var str9 = "wer book wer";alert(reg9.test(str9)); // 打印结果:false</script></head><body></body></html>
index5-字符匹配:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>index5-字符匹配</title><script type="text/javascript">// \s 匹配空白字符、空格、制表符和换行符var reg = /javascript/;var str = "study java script";alert(reg.test(str)); // 打印结果:falsevar reg2 = /java\sscript/;var str2 = "study java script";alert(reg2.test(str2)); // 打印结果:true// | 或者var reg3 = /java|script/;var str3 = "study java";alert(reg3.test(str3)); // 打印结果:true// {min,max} 出现次数限制var reg4 = /java{2,4}/;var str4 = "study javaa script";alert(reg4.test(str4)); // 打印结果:true// (){min,max} 分组出现次数限制var reg5 = /(java){2,4}/;var str5 = "study javajava script";alert(reg5.test(str5)); // 打印结果:truealert(RegExp.$1); // 取第一组的值 打印结果:javavar reg6 = /(.*)\s(.*)/; // .* 任意字符 , 整理意思为 任意字符\s任意字符var str6 = "study js";//alert(reg6.test(str6)); // 打印结果:true// 交换空格前后元素alert(str6.replace(reg6,'$2 $1')); // 打印结果:js study// 贪婪 :+ * ? {m,n} {m,} {m}// 惰性 :+? *? ?? {m,n}? {m,}? {m}?var reg7 = /6(.*)6/;var str7 = "study 6js6 6js6 6js6";alert(str7.replace(reg7,'$1')); // 打印结果:study js6 6js6 6jsvar reg8 = /6(.*?)6/;var str8 = "study 6js6 6js6 6js6";alert(str8.replace(reg8,'$1')); // 打印结果:study js 6js6 6js6var reg9 = /6(.*?)6/g;var str9 = "study 6js6 6js6 6js6";alert(str9.replace(reg9,'$1')); // 打印结果:study js js js</script></head><body></body></html>
index6-exec:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>index6-exec</title><script type="text/javascript">var reg = /[a-z]+\s\d{4}/; // 任意字母开头+四位数字var str = "hello 1234";alert(reg.exec(str)); // 打印结果:hello 1234 (返回匹配的字符串)// 捕获型分组var reg2 = /([a-z]+)\s(\d{4})/;var str2 = "hello 4567";alert(reg2.exec(str2)); // 打印结果:hello 4567,hello,4567// 数组中第一个数据是匹配整个正则表达式的字符串// 数组中第二个数据是第一个分组中的字符串// 数组中第三个数据是第二个分组中的字符串// 非捕获型分组var reg3 = /([a-z]+)\s(?:\d{4})/;var str3 = "hello 4567";alert(reg3.exec(str3)); // 打印结果:hello 4567,hello// 加上 ?: 表示不想捕获// 前瞻捕获var reg4 = /goo(?=gle)/;var str4 = "google";alert(reg4.exec(str4)); // 打印结果:goovar reg5 = /[a-z]+(?=gle)/;var str5 = "google";alert(reg5.exec(str5)); // 打印结果:goo// 换行模式var reg6 = /^\d+/gm;var str6 = "1.baidu\n2.google\n3.js";alert(str6.replace(reg6,"#"));/* 打印结果:#.baidu#.google#.js*/</script></head><body></body></html>
index7-常用验证规则:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>index7-常用验证规则</title><script type="text/javascript">// 验证手机号var reg = /^1[3578]\d{9}/;var phone = "13800138000";alert(reg.test(phone)); // 打印结果:true// 隐藏中间4位var reg2 = /(\d{3})\d{4}(\d{4})/;var phone2 = "13800138000";alert(phone2.replace(reg2,'$1****$2')); // 打印结果:138****8000// 验证是否为压缩包var reg_file = /\w+\.rar|zip|gz/;var file = "test.rar"alert(reg_file.test(file)); // 打印结果:true// 验证邮箱var reg_email = /[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9_\.\-]+\.[a-zA-Z]+$/;var email = "iwanghang@";alert(reg_email.test(email)); // 打印结果:true</script></head><body></body></html>
转载请注明出处:/iwanghang
欢迎移动开发爱好者交流 沈阳或周边城市公司有意开发Android,请与我联系 联系方式 微信:iwanghang QQ:413711276 邮箱:iwanghang@
觉得博文有用,请点赞,请评论,请关注,谢谢!~