一、什么是正则表达式
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head><body><script>// 定义正则表达式// 这个正则表达式表示一个形式:以m开头,n结尾,中间是6位数字var regexp = /^m\d{6}n$/;var str1 = 'm123456n';var str2 = 'm1234567n';var str3 = 'm123b56n';var str4 = '123456';console.log(regexp.test(str1)); // trueconsole.log(regexp.test(str2)); // falseconsole.log(regexp.test(str3)); // falseconsole.log(regexp.test(str4)); // false</script>
</body></html>
二、正则表达式的创建
<script>// 创建正则表达式方法1var regexp1 = /^\d{6}$/;// 创建正则表达式方法2var regexp2 = new RegExp('^\\d{6}$');var str = '555666';console.log(regexp1.test(str)); // trueconsole.log(regexp2.test(str)); // trueconsole.log(typeof regexp1); // objectconsole.log(typeof regexp2); // object</script>
三、元字符
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><script>// 题目1:某快递公司运单号形式是这样的:123-4567-890,请使用正则表达式检查某字符串是否符合此格式// 正则表达式var regexp1 = /^\d\d\d-\d\d\d\d-\d\d\d$/;// 待测试的字符串var str1 = '444-5555-000';console.log(regexp1.test(str1));// 题目2:某产品的验证秘钥形式是这样的:□□□-□□□□-□□□,其中□表示字母数字或者下划线,请使用正则表达式检查某字符串是否符合此格式// 正则表达式var regexp2 = /^\w\w\w-\w\w\w\w-\w\w\w$/;// 待测试的字符串var str2 = 'abc-__ab-123';console.log(regexp2.test(str2));// 题目3:某产品批号形式为:123.45^67#89,请使用正则表达式检查某字符串是否符合此格式var regexp3 = /^\d\d\d\.\d\d\^\d\d\#\d\d$/;// 待测试的字符串var str3 = '666.66^66#66';console.log(regexp3.test(str3));</script>
</body>
</html>
四、方括号表示法
<script>// 某学校的学号规定:第1位是一个字母,b表示本科生,y表示研究生,后面是7位数字,用正则表示为:// 学号字符串var str1 = 'm4444555';// 用正则表达式进行检查console.log(/^[by]\d{7}$/.test(str1));// *******************************************// 题目1:请验证某字符串是否是5位字母,大小写均可var str2 = 'abcde';var str3 = 'abcd5';console.log(/^[a-zA-Z]{5}$/.test(str2));console.log(/^[a-zA-Z]{5}$/.test(str3));// 题目2:请验证某字符串是否是5位,且仅由小写字母、点构成var str4 = 'mnp..';var str5 = 'mnp.#';console.log(/^[a-z\.]{5}$/.test(str4));console.log(/^[a-z\.]{5}$/.test(str5));// 题目3:请验证某字符串是否是4位小写字母,且最后一位不能是m字母var str6 = 'abcd';var str7 = 'abcm';var str8 = 'ab1c';console.log(/^[a-z]{3}[a-ln-z]$/.test(str6));console.log(/^[a-z]{3}[a-ln-z]$/.test(str7));console.log(/^[a-z]{3}[a-ln-z]$/.test(str8));</script>
五、量词
<script>// 题目1:请验证字符串是否符合手机号码的规则:11位数字,并且肯定以1开头var str1 = '13812345678';var str2 = '138123456789';var str3 = '38123456789';var regexp1 = /^1\d{10}$/;console.log(regexp1.test(str1));console.log(regexp1.test(str2));console.log(regexp1.test(str3));// 题目2:请验证某字符串是否是这样的:以字母开头,中间是任意位数字(最少1位)构成,并以字母结尾var str4 = 'a123123123b';var str5 = 'abcd';var str6 = 'a1b';var regexp2 = /^[a-zA-Z]\d+[a-zA-Z]$/;console.log(regexp2.test(str4));console.log(regexp2.test(str5));console.log(regexp2.test(str6));// 题目3:请验证某字符串是否符合网址规则:以www.开头,中间是任意位的字符(字母数字下划线,最少一位),最后以.com结尾,也可以以.com.cn结尾var str7 = 'www.imooc.com';var str8 = 'www.sina.com.cn';var str9 = 'www.news.cn';var regexp3 = /^www\.\w+\.com(\.cn)?$/;console.log(regexp3.test(str7));console.log(regexp3.test(str8));console.log(regexp3.test(str9));</script>
六、修饰符
七、正则表达式的相关方法
<script>var str = 'abc123def456ghi789';var regexp = /\d+/g; // +表示贪婪的,尽可能多的匹配var result1 = regexp.exec(str);var result2 = regexp.exec(str);var result3 = regexp.exec(str);var result4 = regexp.exec(str);console.log(result1);console.log(result2);console.log(result3);console.log(result4);// 使用循环语句来循环执行exec,寻找所有的匹配结果var result;while (result = regexp.exec(str)) {console.log(result);}</script>
八、字符串的相关方法
<script>var str = 'abc123def4567ghi89';// search()方法,很像indexOf(),返回查找到的第一个下标,如果找不到就是-1var result1 = str.search(/\d+/g);var result2 = str.search(/m/g); //寻找字母mconsole.log(result1); // 3console.log(result2); // -1// match()方法,返回查找到的数组,找不到就是nullvar result3 = str.match(/\d+/g);console.log(result3); // ["123", "4567", "89"]// replace()方法,进行替换var result4 = str.replace(/[a-z]+/g, '*'); // 注意+表示贪婪的,尽可能多的连续匹配小写字母console.log(result4); // *123*4567*89// split()方法,进行字符串拆为数组var result5 = str.split(/\d+/g);console.log(result5); // ["abc", "def", "ghi", ""]</script>
九、正则表达式进行表单验证
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.warning {color: red;display: none;}</style>
</head><body><div><p>请输入中文姓名:<input type="text" id="nameField"><span class="warning" id="nameWarning">输入的姓名不合法</span></p><p>请输入手机号码:<input type="text" id="telField"><span class="warning" id="telWarning">输入的手机号码不合法</span></p><p>请输入Email:<input type="text" id="emailField"><span class="warning" id="emailWarning">输入的Email不合法</span></p></div><script>var nameField = document.getElementById('nameField');var telField = document.getElementById('telField');var emailField = document.getElementById('emailField');var nameWarning = document.getElementById('nameWarning');var telWarning = document.getElementById('telWarning');var emailWarning = document.getElementById('emailWarning');// 当文本框失去焦点,就是光标不在文本框中了nameField.onblur = function () {// 得到姓名var name = nameField.value;if (/^[\u4E00-\u9FA5]{2,4}$/.test(name)) {// 如果通过校验nameWarning.style.display = 'none';} else {// 如果没有通过校验nameWarning.style.display = 'inline';}};telField.onblur = function () {// 得到电话var tel = telField.value;if (/^1\d{10}$/.test(tel)) {// 如果通过校验telWarning.style.display = 'none';} else {// 如果没有通过校验telWarning.style.display = 'inline';}};emailField.onblur = function () {// 得到emailvar email = emailField.value;// 合法的email都是abc_def123@abc.netif (/^\w{2,}\@\w{2,}\.[a-z]{2,4}(\.[a-z]{2,4})?$/.test(email)) {// 如果通过校验emailWarning.style.display = 'none';} else {// 如果没有通过校验emailWarning.style.display = 'inline';}};</script>
</body></html>