1234567891011121314151617181920212223 |
- <!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 名称 = /需要校验的规则/
- const reg = /太阳/g;
- // 1.test() 匹配文中是否包含校验的字符 返回布尔值
- console.log(reg.test("今天没有雨"));
- // 2.exec() 返回数组格式 查找符合条件的字符
- console.log(reg.exec("今天没有太阳,明天有太阳"));
- // 3.replace() 替换指定字符
- const str = '今天没有太阳,明天有太阳';
- console.log(str.replace(reg,'雨'));
- // 4.match() 在字符串中查找符合规则的字符
- console.log(str.match(reg));
- </script>
- </body>
- </html>
|