|
@@ -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>
|
|
|
|
|
+ // 正则:const 名称 = /规则/;
|
|
|
|
|
+ const reg = /西游记/g;
|
|
|
|
|
+ // 1.test 匹配文本中是否包含校验的字符 返回布尔值
|
|
|
|
|
+ const str = '红楼梦是四大名著之一';
|
|
|
|
|
+ console.log(reg.test(str));
|
|
|
|
|
+ // 2.exec() 返回数组格式 查找符合条件的字符
|
|
|
|
|
+ const res = reg.exec("西游记是一本神话故事,我喜欢看西游记");
|
|
|
|
|
+ console.log(res);
|
|
|
|
|
+ // 3.replace 替换指定字符
|
|
|
|
|
+ const str1 = '西游记是四大名著之一';
|
|
|
|
|
+ const res1 = str1.replace(reg,'红楼梦');
|
|
|
|
|
+ console.log(res1);
|
|
|
|
|
+ // 4.match 在字符串中查找符合规则的正则
|
|
|
|
|
+ const str2 = '西游记是一本神话故事,我喜欢看西游记';
|
|
|
|
|
+ const res2 = str2.match(reg);
|
|
|
|
|
+ console.log(res2);
|
|
|
|
|
+ </script>
|
|
|
|
|
+</body>
|
|
|
|
|
+</html>
|