瀏覽代碼

fix:正则

e 1 年之前
父節點
當前提交
6ec7c14453

+ 31 - 0
正则/1.正则表达式.html

@@ -0,0 +1,31 @@
+<!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 = /西游记/g;
+
+        // 1.test()
+        // const res = reg.test("红楼梦是四大名著之一");
+        // console.log(res)
+
+        // 2.exec()
+        const res = reg.exec("学习四大名著--西游记")
+        console.log(res)
+
+        // 3.replace()
+        // const str = '西游记是一本好书,西游记是四大名著之一';
+        // const res = str.replace(reg,'红楼梦')
+        // console.log(res)
+
+        // 4.match()
+        // const str = '西游记是一本好书,西游记是四大名著之一'
+        // const res = str.match(reg);
+        // console.log(res)
+    </script>
+</body>
+</html>

+ 21 - 0
正则/2.修饰符.html

@@ -0,0 +1,21 @@
+<!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/i;
+        // console.log(reg.test("abc"))
+        // console.log(reg.test('a')) 
+        // console.log(reg.test("Abc"))
+
+        const reg = /react/gi;
+        const str = '学习react,React好,REACT棒';
+        console.log(str.replace(reg,'vue'))
+
+    </script>
+</body>
+</html>

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

@@ -0,0 +1,38 @@
+<!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>
+        // 单词边界 \b
+        // const reg = /\bcat\b/g;
+        // const str = 'This is a cat it is a scattered';
+        // console.log(str.replace(reg,'dog'))
+
+
+        // ^ 匹配字母行首
+        // const reg = /^ac/i;
+        // console.log(reg.test('acc'))
+        // console.log(reg.test("bca"))
+        // console.log(reg.test("Abca"))
+
+
+        // $ 匹配字母行尾
+        // const reg = /c$/;
+        // console.log(reg.test('ccc'))
+        // console.log(reg.test(''))
+        // console.log(reg.test('cb'))
+        // console.log(reg.test('ac'))
+
+        const reg = /^a$/;
+        console.log(reg.test('a a'))
+        console.log(reg.test(''))
+        console.log(reg.test('aba'))
+        console.log(reg.test('bbb'))
+
+    </script>
+</body>
+</html>

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

@@ -0,0 +1,54 @@
+<!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>
+        // // *: 0次或者更多次
+        // const reg = /a*$/;
+        // console.log(reg.test("a"))
+        // console.log(reg.test(""))
+        // console.log(reg.test("bdccad"))
+        // console.log(reg.test("bbaca"))
+
+        // // +: 1次或者更多次
+        // const reg = /a+$/;
+        // console.log(reg.test("a"))
+        // console.log(reg.test(""))
+        // console.log(reg.test("bdccad"))
+        // console.log(reg.test("bbaca"))
+
+        // // ? 0次或者1次
+        // const reg = /a?$/;
+        // console.log(reg.test("a"))
+        // console.log(reg.test(""))
+        // console.log(reg.test("aaa"))
+        // console.log(reg.test("bbb"))
+
+        // // {n} 只能有n次
+        // const reg = /a{1}$/;
+        // console.log(reg.test("a"))
+        // console.log(reg.test(""))
+        // console.log(reg.test("aaac"))
+        // console.log(reg.test("bbb"))
+
+        // {n,m} 从n-m次
+        // const reg = /a{1,3}$/;
+        // console.log(reg.test("a"))
+        // console.log(reg.test(""))
+        // console.log(reg.test("aaa"))
+        // console.log(reg.test("bbb"))
+        
+        // {n,} 大于或等于n次
+        const reg = /a{2,}$/;
+        console.log(reg.test("a"))
+        console.log(reg.test(""))
+        console.log(reg.test("aaa"))
+        console.log(reg.test("bbb"))
+
+    </script>
+</body>
+</html>

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

@@ -0,0 +1,36 @@
+<!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]/ //任意匹配abc
+        // console.log(reg.test('abc'))
+        // console.log(reg.test('abandon'))
+        // console.log(reg.test('banana'))
+        // console.log(reg.test('car'))
+        // console.log(reg.test('listen'))
+        // console.log(reg.test('tomorrow'))
+
+        // 连字符
+        // const reg1 = /[a-z]/;//a-z的26个字母任意匹配
+        // const reg2 = /[A-Z]/; //A-Z的26个字母任意匹配
+        // const reg3 = /[0-9]/; //0-9的数字任意匹配
+        // const reg4 = /[a-zA-Z0-9_]/
+
+        // . 除换行外的任意字符
+        const reg = /./
+        console.log(reg.test(''))
+        console.log(reg.test('abc'))
+        console.log(reg.test('\n'))
+        console.log(reg.test('\r'))
+        console.log(reg.test('34567'))
+
+
+    </script>
+</body>
+</html>

+ 31 - 0
正则/6.分组和分支结构.html

@@ -0,0 +1,31 @@
+<!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)+/
+        // console.log(reg.test('ab'))
+        // console.log(reg.test('abab'))
+        // console.log(reg.test('aabbab'))
+
+        // const reg = /^(\d{4})-(\d{2})-(\d{2})$/
+        // const data = '2023-12-13';
+        // // console.log(reg.test(data))
+        // // 12/13/2023
+        // console.log(data.replace(reg,'$2-$3-$1'))
+        const reg = /西游记|红楼梦/
+        const str1 = '西游记'
+        const str2 = '水浒传'
+        const str3 = '红楼梦'
+        const str4 = '三国演义'
+        console.log(reg.test(str1))
+        console.log(reg.test(str2))
+        console.log(reg.test(str3))
+        console.log(reg.test(str4))
+    </script>
+</body>
+</html>

+ 28 - 0
正则/7.案例.html

@@ -0,0 +1,28 @@
+<!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.手机号脱敏 13748576543 => 137****6543
+        // const str = '13748576543';
+        // const reg = /^(1[3-9]\d)\d{4}(\d{4})$/;
+        // console.log(str.replace(reg,'$1****$2'))
+
+        // 2.密码匹配 (6-16字母,数字,下划线)
+        // const reg = /^\w{6,16}$/
+        // // const reg = /^[a-zA-Z0-9_]{6,16}$/
+        // console.log(reg.test("hello12345"))
+
+        // 3.匹配16进制颜色
+        // const reg = /^#([0-9a-fA-F]{6})|([0-9a-fA-F]{3})$/
+        // console.log(reg.test("#ff0000"))
+        // console.log(reg.test("#ff0"))
+
+        // 匹配24小时时间 23:59
+    </script>
+</body>
+</html>