zheng 17 ore fa
parent
commit
7db6ed4ec1

+ 29 - 0
13.正则/3.元字符-边界符.html

@@ -0,0 +1,29 @@
+<!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.单词边界 \b
+        var reg = /\bab\b/gi;
+        const str = 'Ab apple,aba sda';
+        console.log(str.match(reg));
+
+        // 2.匹配字母首行
+        const reg1 = /^ab/;
+        console.log(reg1.test("abbab"));
+
+        // 3.匹配字母尾行
+        const reg2 = /ab$/;
+        console.log(reg2.test("abbabb"));
+
+        const reg3 = /^a$/;
+        console.log(reg3.test("aaa"));
+        console.log(reg3.test("a"));
+        console.log(reg3.test(" "));
+    </script>
+</body>
+</html>

+ 16 - 0
13.正则/4.元字符-量词.html

@@ -0,0 +1,16 @@
+<!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 = /a{1,}$/;
+        console.log(reg.test("aa"));
+        console.log(reg.test(" "));
+        console.log(reg.test("aab"));
+    </script>
+</body>
+</html>

+ 35 - 0
13.正则/5.字符类.html

@@ -0,0 +1,35 @@
+<!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("ababba"));
+        // console.log(reg.test("abc"));
+        // console.log(reg.test("acbab"));
+        // console.log(reg.test("acacaca"));
+        // console.log(reg.test("ddfffdf"));
+
+        // 连字符 -
+        // const reg1 = /0-9/;
+        // console.log(reg1.test('121s'));
+
+        // // 取反 ^
+        // const reg2 = /[^a-z]/;
+        // console.log(reg2.test('32'));
+
+        // . 匹配换行外的任意字符
+        const reg3 = /./;
+        console.log(reg3.test("Aaaa"));
+        console.log(reg3.test("1234578765432"));
+        console.log(reg3.test("")); // false
+        console.log(reg3.test("\n"));
+        console.log(reg3.test("\r"));
+    </script>
+</body>
+</html>

+ 17 - 0
13.正则/6.分组.html

@@ -0,0 +1,17 @@
+<!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 data = '2026-03-17';
+        const reg = /^(\d{4})-(\d{2})-(\d{2})$/;
+        console.log(reg.test(data));
+        // 03/17/2026
+        console.log(data.replace(reg,'$2/$3/$1'));
+    </script>
+</body>
+</html>

+ 19 - 0
13.正则/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>