郑柏铃 1 day ago
parent
commit
babd1cffb6
3 changed files with 96 additions and 0 deletions
  1. 37 0
      23.正则/5.元字符-字符类.html
  2. 40 0
      23.正则/6.分组.html
  3. 19 0
      23.正则/7.案例.html

+ 37 - 0
23.正则/5.元字符-字符类.html

@@ -0,0 +1,37 @@
+<!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>
+        // [] 任意匹配
+        // const reg = /[abc]/;
+        // console.log(reg.test("a"))
+        // console.log(reg.test("ababab"))
+        // console.log(reg.test("abc"))
+        // console.log(reg.test("acbacb"))
+        // console.log(reg.test("acacac"))
+        // console.log(reg.test("dddfff"))
+        // // - 连字符
+        // const reg1 = /0-9/;
+        // const reg2 = /a-z/;
+        // const reg3 = /A-Z/;
+        // const reg4 = /a-zA-Z0-9_/;
+
+        // // ^ 取反
+        // const reg5 = /\D/;
+        // console.log(reg5.test('ab'));
+
+        // . 除换行外的任意字符
+        const reg6 = /./;
+        console.log(reg6.test("aaa"));
+        console.log(reg6.test("1234567890"));
+        console.log(reg6.test(""));
+        console.log(reg6.test("\n"));
+        console.log(reg6.test("\r"));
+    </script>
+</body>
+</html>

+ 40 - 0
23.正则/6.分组.html

@@ -0,0 +1,40 @@
+<!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>
+        // 分组
+        const reg = /(ab)(cd)/;
+        console.log(reg.test("abababacd"));
+        console.log(reg.test("abab"));
+        console.log(reg.test("abecd"));
+        // 分组捕获
+        const data = '2025-08-19';
+        const reg1 = /^(\d{4})-(\d{2})-(\d{2})$/;
+        console.log(reg1.test(data))
+        console.log(data.replace(reg1,'$2/$3/$1'))
+
+        // 分支结构
+        const a1 = '西游记';
+        const a2 = '水浒传';
+        const a3 = '红楼梦';
+        const a4 = '三国演义';
+
+        const reg2 = /西游记|红楼梦/;
+        console.log(reg2.test(a1));
+        console.log(reg2.test(a2));
+        console.log(reg2.test(a3));
+        console.log(reg2.test(a4));
+
+        /**
+         * 1.安装插件any-rule
+         * 2.https://regexper.com/
+         * 可视化表达式
+         */
+    </script>
+</body>
+</html>

+ 19 - 0
23.正则/7.案例.html

@@ -0,0 +1,19 @@
+<!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.手机号脱敏 13789430987 => 137****0987
+        // 2.密码匹配 (6-16字母、数字、下划线)
+        // 3.匹配16进制颜色 #ff0000 #0f0
+        // 4.匹配24小时时间 23:59 20:12 08:35 18:22
+    </script>
+</body>
+
+</html>