-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJS2-5.html
1441 lines (851 loc) · 54.6 KB
/
JS2-5.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head hexo-theme='https://volantis.js.org/#2.1.2'>
<meta charset="utf-8">
<!-- SEO相关 -->
<!-- 渲染优化 -->
<meta name="renderer" content="webkit">
<meta name="force-rendering" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
<meta name="HandheldFriendly" content="True" >
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<!-- 页面元数据 -->
<title>JS正则 - CRainyDay</title>
<meta name="keywords" content="JS">
<meta name="description" content="JS中的正则">
<!-- feed -->
<!-- import meta -->
<!-- link -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/[email protected]/css/all.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/[email protected]/dist/jquery.fancybox.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/waves.min.css">
<link rel="shortcut icon" type='image/x-icon' href="https://cdn.jsdelivr.net/gh/crainyday/blog@master/favicon.ico">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/APlayer.min.css">
<!-- import link -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/crainyday/blog@master/css/style.css">
<script>
function setLoadingBarProgress(num) {
document.getElementById('loading-bar').style.width=num+"%";
}
</script>
<!-- ba -->
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?bf368df5385d5be3472f3155e4cdb303";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
</head>
<body>
<div id="loading-bar-wrapper">
<div id="loading-bar"></div>
</div>
<header class="l_header shadow floatable blur">
<div class='wrapper'>
<div class='nav-sub container--flex'>
<a class="logo flat-box"></a>
<ul class='switcher h-list'>
<li><a class="s-comment flat-btn fas fa-comments fa-fw" target="_self" href='javascript:void(0)'></a></li>
<li><a class="s-toc flat-btn fas fa-list fa-fw" target="_self" href='javascript:void(0)'></a></li>
</ul>
</div>
<div class="nav-main container container--flex">
<a class="logo flat-box" target="_self" href='/'>
<i class='fas fa-home'></i>
CRainyDay
</a>
<div class='menu navigation'>
<ul class='h-list'>
<li>
<a class="flat-box" href=/categories/
id="categories"
>
<i class='fas fa-folder-open fa-fw'></i>
分类
</a>
</li>
<li>
<a class="flat-box" href=/archives/
id="archives"
>
<i class='fas fa-archive fa-fw'></i>
归档
</a>
</li>
<li>
<a class="flat-box" href=/tags/
id="tags"
>
<i class='fas fa-tags fa-fw'></i>
标签
</a>
</li>
<li>
<a class="flat-box" href=/friends/
id="friends"
>
<i class='fas fa-link fa-fw'></i>
友链
</a>
</li>
<li>
<a class="flat-box"
>
<i class='fas fa-ellipsis-v fa-fw'></i>
更多
</a>
<ul class="submenu">
<li>
<a class="flat-box" href=/about/music.html
id="aboutmusic.html"
>
<i class='fas fa-music fa-fw'></i>
往后余生
</a>
</li>
<hr>
<li>
<a class="flat-box" href=/bbs/
id="bbs"
>
<i class='fas fa-comment-alt fa-fw'></i>
留言
</a>
</li>
<hr>
<li>
<a class="flat-box" href=/about/
id="about"
>
<i class='fas fa-info-circle fa-fw'></i>
关于
</a>
</li>
</ul>
</li>
</ul>
</div>
<div class="m_search">
<form name="searchform" class="form u-search-form">
<i class="icon fas fa-search fa-fw"></i>
<input type="text" class="input u-search-input" placeholder="搜索" />
</form>
</div>
<ul class='switcher h-list'>
<li><a class="s-search flat-btn fas fa-search fa-fw" target="_self" href='javascript:void(0)'></a></li>
<li><a class="s-menu flat-btn fas fa-bars fa-fw" target="_self" href='javascript:void(0)'></a></li>
</ul>
</div>
</div>
</header>
<ul class="menu-phone navigation white-box">
<li>
<a class="flat-box" href=/
id="home"
>
<i class='fas fa-home fa-fw'></i>
博客
</a>
</li>
<li>
<a class="flat-box" href=/archives/
id="archives"
>
<i class='fas fa-archive fa-fw'></i>
归档
</a>
</li>
<li>
<a class="flat-box" href=/categories/
id="categories"
>
<i class='fas fa-folder-open fa-fw'></i>
分类
</a>
</li>
<li>
<a class="flat-box" href=/friends/
id="friends"
>
<i class='fas fa-link fa-fw'></i>
友链
</a>
</li>
<li>
<a class="flat-box" href=/bbs/
id="bbs"
>
<i class='fas fa-comment-alt fa-fw'></i>
留言
</a>
</li>
<li>
<a class="flat-box" href=/about/
id="about"
>
<i class='fas fa-info-circle fa-fw'></i>
关于
</a>
</li>
</ul>
<script>setLoadingBarProgress(40);</script>
<div class="l_body nocover">
<div class='body-wrapper'>
<div class='l_main'>
<article id="post" class="post white-box shadow floatable article-type-post" itemscope itemprop="blogPost">
<section class='meta'>
<a title='JS正则' href='/JS2-5.html'><img class='thumbnail' src='https://cdn.jsdelivr.net/gh/crainyday/blog@master/imgs/more/more01.png'></a>
<div class="meta" id="header-meta">
<h1 class="title">
<a href="/JS2-5.html">
JS正则
</a>
</h1>
<div class='new-meta-box'>
<div class='new-meta-item author'>
<a href="https://crainyday.gitee.io" rel="nofollow">
<img src="https://cdn.jsdelivr.net/gh/crainyday/blog@master/lazyload.svg" data-original="https://cdn.jsdelivr.net/gh/crainyday/blog@master/avatar.png">
<p>crainyday</p>
</a>
</div>
<div class='new-meta-item category'>
<a href='/categories/%E5%89%8D%E7%AB%AF/JS/' rel="nofollow">
<i class="fas fa-folder-open" aria-hidden="true"></i>
<p>前端 / JS</p>
</a>
</div>
<div class="new-meta-item date">
<a class='notlink'>
<i class="fas fa-edit" aria-hidden="true"></i>
<p>发布于:2019年4月4日</p>
</a>
</div>
<div class="new-meta-item wordcount">
<a class='notlink'>
<i class="fas fa-keyboard" aria-hidden="true"></i>
<p>字数:2.2k字</p>
</a>
</div>
<div class="new-meta-item readtime">
<a class='notlink'>
<i class="fas fa-hourglass-half" aria-hidden="true"></i>
<p>时长:8分钟</p>
</a>
</div>
</div>
<hr>
</div>
</section>
<section class="article typo">
<div class="article-entry" itemprop="articleBody">
<p>JS中的正则</p>
<a id="more"></a>
<h2 id="一-正则表达式简介">一.正则表达式简介</h2>
<blockquote>
<p>百度百科:正则表达式, 又称规则表达式. (英语: Regular Expression, 在代码中常简写为regex、regexp或RE), 计算机科学的一个概念. 正则表达式通常被用来检索、替换那些符合某个模式(规则)的文本.</p>
<p>正则表达式用于定义一些字符串的规则, 计算机根据正则表达式, 来检查是否符合规则, 获取、提取字符串中符合规则的内容.</p>
</blockquote>
<h3 id="1-创建RegExp对象">1.创建RegExp对象</h3>
<p>正则表达式对象即RegExp对象, JS内置的对象.</p>
<p>RegExp对象的test()方法, 检索字符串中指定的值, 返回 true 或 false</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//语法:</span></span><br><span class="line"><span class="comment">//var 变量名 = new RegExp("正则表达式","匹配模式");</span></span><br><span class="line"><span class="comment">//匹配模式:i 忽略大小写;g 全局匹配;m 多行匹配;</span></span><br><span class="line"><span class="keyword">var</span> reg = <span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">"a"</span>,<span class="string">"i"</span>);</span><br><span class="line"><span class="keyword">var</span> str = <span class="string">"ABCD"</span>;</span><br><span class="line"><span class="keyword">var</span> result = reg.test(str);<span class="comment">//检查字符串中是否含A</span></span><br><span class="line"><span class="built_in">console</span>.log(result);<span class="comment">//true</span></span><br></pre></td></tr></table></figure>
<h3 id="2-字面量创建RegExp对象">2.字面量创建RegExp对象</h3>
<p>字面量的方式创建更加简单, 使用构造函数创建更加灵活(可以传参数).</p>
<p><font style = 'font-size:1.2em;'><center>语法: var 变量名 = /正则表达式/匹配模式;</center></font></p>
<p><font style = 'font-size:1.2em;color:red;'>注意: 语法中没有引号</font></p>
<table>
<thead>
<tr>
<th>表达式</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>[abc]</td>
<td>查找方括号之间的任何字符</td>
</tr>
<tr>
<td>[^abc]</td>
<td>查找任何不在方括号之间的字符</td>
</tr>
<tr>
<td>[0-9]</td>
<td>查找任何从 0 至 9 的数字</td>
</tr>
<tr>
<td>[a-z]</td>
<td>查找任何从小写 a 到小写 z 的字符</td>
</tr>
<tr>
<td>[A-Z]</td>
<td>查找任何从大写 A 到大写 Z 的字符</td>
</tr>
<tr>
<td>[A-z]</td>
<td>查找任何从大写 A 到小写 z 的字符</td>
</tr>
<tr>
<td>[adgk]</td>
<td>查找给定集合内的任何字符</td>
</tr>
<tr>
<td>[^adgk]</td>
<td>查找给定集合外的任何字符</td>
</tr>
<tr>
<td>(red blue green)</td>
<td>查找任何指定的选项</td>
</tr>
</tbody>
</table>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> reg = <span class="regexp">/a/i</span>;<span class="comment">//等价于:var reg = new RegExp("a","i");</span></span><br><span class="line"><span class="comment">//1.创建一个正则表达式检查一个字符串中是否含有a或b</span></span><br><span class="line">reg = <span class="regexp">/a|b/</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//2.创建一个正则表达式检查一个字符串中是否含有字母</span></span><br><span class="line">reg = <span class="regexp">/[A-z]/</span>;<span class="comment">//中括号表示或的意思</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//3.检查一个字符串中是否含有abc或adc或aec</span></span><br><span class="line">reg = <span class="regexp">/a[bde]c/</span>;<span class="comment">//以a开头,c结尾,中间为b或d或e</span></span><br><span class="line"></span><br><span class="line">reg = <span class="regexp">/[^0-9]/</span>;<span class="comment">//检查除了数字之外的内容</span></span><br><span class="line">reg = <span class="regexp">/(abc)/</span>;<span class="comment">//abc连续出现</span></span><br><span class="line">reg = <span class="regexp">/(a|b|c)/</span>;<span class="comment">//出现过a或b或c</span></span><br></pre></td></tr></table></figure>
<h2 id="二-字符串与正则相关的方法">二.字符串与正则相关的方法</h2>
<h3 id="1-split">1.split</h3>
<ul>
<li>根据参数将字符串拆分为字符数组</li>
<li>参数可以是正则表达式</li>
<li>正则不需要设置全局匹配, split默认将所有符合条件的拆分</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">"1a2b3c4d5e6f7g8"</span>;</span><br><span class="line"><span class="keyword">var</span> arr = str.split(<span class="regexp">/[A-z]/</span>);<span class="comment">//根据任意字母拆分</span></span><br><span class="line"><span class="built_in">console</span>.log(arr);<span class="comment">//1,2,3,4,5,6,7,8</span></span><br></pre></td></tr></table></figure>
<h3 id="2-search">2.search</h3>
<ul>
<li>检索与正则表达式相匹配的值</li>
<li>如果可以搜索到指定内容, 则返回第一次出现过的位置, 没搜索到返回-1</li>
<li>它能接受一个正则表达式作为参数, 然后根据正则表达式检索字符串</li>
<li>search正则查找时不能设置全局匹配</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//功能和indexOf()类似</span></span><br><span class="line"><span class="keyword">var</span> str = <span class="string">"hello abc hello adc hello aec"</span>;</span><br><span class="line"><span class="comment">//搜索字符串中是否含有abc或adc或aec</span></span><br><span class="line"><span class="keyword">var</span> result = str.search(<span class="regexp">/a[bde]c/</span>);</span><br><span class="line"><span class="built_in">console</span>.log(result);<span class="comment">//6</span></span><br></pre></td></tr></table></figure>
<h3 id="3-match">3.match</h3>
<ul>
<li>可以根据正则表达式, 从一个字符串中将符合条件的内容提取出来</li>
<li>默认情况下, match只会找第一个符合条件的内容, 找到后停止检索, 我们可以将正则表达式设置为全局匹配模式, 这样match会找到所有符合条件的内容</li>
<li>可以为正则表达式设置多个匹配模式, 且顺序无所谓</li>
<li>match会将匹配到的内容封装到一个数组中返回, 即使只查询到一个结果.</li>
<li>可以将一个没有规则的长串中, 有规则的内容提取出来, 很实用</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">"1A2B3C4d5e6f7g8"</span>;</span><br><span class="line"><span class="keyword">var</span> result = str.match(<span class="regexp">/[a-z]/gi</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Array</span>.isArray(result));<span class="comment">//true;</span></span><br><span class="line"><span class="built_in">console</span>.log(result);<span class="comment">//A,B,C,d,e,f,g</span></span><br><span class="line"><span class="built_in">console</span>.log(result[<span class="number">0</span>]);<span class="comment">//A</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//筛选出所有的QQ号</span></span><br><span class="line"><span class="keyword">var</span> str = <span class="string">"826720408abhfe826720407"</span>+</span><br><span class="line"> <span class="string">"freaf826720405C826720"</span>+</span><br><span class="line"> <span class="string">"404d826720405e8267204"</span>+</span><br><span class="line"> <span class="string">"00f826720409g826720401"</span>;</span><br><span class="line"><span class="keyword">var</span> result = str.match(<span class="regexp">/[0-9]{8,10}/g</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Array</span>.isArray(result));<span class="comment">//true;</span></span><br><span class="line"><span class="built_in">console</span>.log(result);</span><br></pre></td></tr></table></figure>
<h3 id="4-replace">4.replace</h3>
<ul>
<li>将字符串中指定的内容替换为新的内容, 不影响原字符串</li>
<li>参数:①被替换的内容, 可以接受一个正则表达式作为参数</li>
<li>②新的内容</li>
<li>默认只会替换一个,可以为正则表达式设置全局匹配模式替换所有内容</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">"1A2B3C4d5e6f7g8"</span>;</span><br><span class="line"><span class="comment">//将所有字母替换为""</span></span><br><span class="line"><span class="keyword">var</span> result = str.replace(<span class="regexp">/[a-z]/gi</span>,<span class="string">""</span>);</span><br><span class="line"><span class="built_in">console</span>.log(result);<span class="comment">//12345678</span></span><br></pre></td></tr></table></figure>
<h2 id="三-正则表达式语法补充">三.正则表达式语法补充</h2>
<h3 id="1-量词">1.量词</h3>
<ul>
<li>量词: 可以设置一个内容出现的次数, 它只对它前面的一个内容起作用</li>
</ul>
<table>
<thead>
<tr>
<th style="text-align:left">量词</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">n+</td>
<td>匹配任何包含至少一个 n 的字符串, 相当于n{1, }</td>
</tr>
<tr>
<td style="text-align:left">n*</td>
<td>匹配任何包含零个或多个 n 的字符串, 相当于n{0, }</td>
</tr>
<tr>
<td style="text-align:left">n?</td>
<td>匹配任何包含零个或一个 n 的字符串, 相当于n{0,1}</td>
</tr>
<tr>
<td style="text-align:left">n{x}</td>
<td>匹配包含 x 个 n 的序列的字符串</td>
</tr>
<tr>
<td style="text-align:left">n{x,y}</td>
<td>匹配包含 x ~ y 个 n 的序列的字符串</td>
</tr>
<tr>
<td style="text-align:left">n{x, }</td>
<td>匹配包含至少 x 个 n 的序列的字符串</td>
</tr>
<tr>
<td style="text-align:left">n$</td>
<td>匹配任何结尾为 n 的字符串</td>
</tr>
<tr>
<td style="text-align:left">^n</td>
<td>匹配任何开头为 n 的字符串</td>
</tr>
<tr>
<td style="text-align:left">?=n</td>
<td>匹配任何其后紧接指定字符串 n 的字符串</td>
</tr>
<tr>
<td style="text-align:left">?!n</td>
<td>匹配任何其后没有紧接指定字符串 n 的字符串</td>
</tr>
</tbody>
</table>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> reg = <span class="regexp">/a{3}/</span>;</span><br><span class="line"><span class="built_in">console</span>.log(reg.test(<span class="string">"xaaax"</span>));<span class="comment">//只有字符串中连续出现3个a时为true</span></span><br><span class="line">reg = <span class="regexp">/(ab){3}/</span>;</span><br><span class="line"><span class="built_in">console</span>.log(reg.test(<span class="string">"xabababx"</span>));<span class="comment">//只有字符串中连续出现3个ab时为true</span></span><br><span class="line">reg = <span class="regexp">/ab{3,5}c/</span>;</span><br><span class="line"><span class="built_in">console</span>.log(reg.test(<span class="string">"xabbbbcx"</span>));<span class="comment">//只有字符串中a和c之间有3~5个b时为true</span></span><br><span class="line">reg = <span class="regexp">/ab{3,}c/</span>;</span><br><span class="line"><span class="built_in">console</span>.log(reg.test(<span class="string">"xabbbcx"</span>));<span class="comment">//只有字符串中a和c之间有2个以上b时为true</span></span><br><span class="line"></span><br><span class="line">reg = <span class="regexp">/^a$/</span>;</span><br><span class="line"><span class="built_in">console</span>.log(reg.test(<span class="string">"a"</span>));<span class="comment">//只有字符串为"a"时,结果为true</span></span><br><span class="line">reg = <span class="regexp">/^a|a$/</span>;</span><br><span class="line"><span class="built_in">console</span>.log(reg.test(<span class="string">"abc"</span>));<span class="comment">//字符串以a开头或以a结尾时为true</span></span><br></pre></td></tr></table></figure>
<p><font style='color:red;font-size:1.2em;font-weight:blod;'>注意: 在正则表达式中同时使用了 ^ 和 $ 量词, 则要求字符串必须完全符合正则表达式.</font></p>
<h3 id="练习-手机号的正则表达式">练习: 手机号的正则表达式</h3>
<p>匹配手机号的正则表达式: <font style='color:red;font-size:1.2em;font-weight:bold;'>/^1[3-9][0-9]{9}$/</font></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*手机号的规则:</span></span><br><span class="line"><span class="comment"> *1. 以1开头</span></span><br><span class="line"><span class="comment"> *2. 第二位为3-9任意数字</span></span><br><span class="line"><span class="comment"> *3. 第三位之后为任意数字9个</span></span><br><span class="line"><span class="comment"> * ^ 和 $ 量词是必须的</span></span><br><span class="line"><span class="comment"> * ^1 [3-9] [0-9]{9}$</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">var</span> phoneNumReg = <span class="regexp">/^1[3-9][0-9]{9}$/</span>;</span><br><span class="line"><span class="built_in">console</span>.log(phoneNumReg.test(<span class="string">"15555555555"</span>));<span class="comment">//true</span></span><br></pre></td></tr></table></figure>
<h3 id="2-元字符">2.元字符</h3>
<table>
<thead>
<tr>
<th>元字符</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>.</td>
<td>查找单个字符, 除了换行和行结束符</td>
</tr>
<tr>
<td>\w</td>
<td>查找单词字符(字母、数字、下划线)</td>
</tr>
<tr>
<td>\W</td>
<td>查找非单词字符</td>
</tr>
<tr>
<td>\d</td>
<td>查找数字</td>
</tr>
<tr>
<td>\D</td>
<td>查找非数字字符</td>
</tr>
<tr>
<td>\s</td>
<td>查找空白字符</td>
</tr>
<tr>
<td>\S</td>
<td>查找非空白字符</td>
</tr>
<tr>
<td>\b</td>
<td>匹配单词边界</td>
</tr>
<tr>
<td>\B</td>
<td>匹配非单词边界</td>
</tr>
<tr>
<td>\0</td>
<td>查找 NULL 字符</td>
</tr>
<tr>
<td>\n</td>
<td>查找换行符</td>
</tr>
<tr>
<td>\f</td>
<td>查找换页符</td>
</tr>
<tr>
<td>\r</td>
<td>查找回车符</td>
</tr>
<tr>
<td>\t</td>
<td>查找制表符</td>
</tr>
<tr>
<td>\v</td>
<td>查找垂直制表符</td>
</tr>
<tr>
<td>\xxx</td>
<td>查找以八进制数 xxx 规定的字符</td>
</tr>
<tr>
<td>\xdd</td>
<td>查找以十六进制数 dd 规定的字符</td>
</tr>
<tr>
<td>\uxxxx</td>
<td>查找以十六进制数 xxxx 规定的 Unicode 字符</td>
</tr>
</tbody>
</table>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//查找字符串中是否有.</span></span><br><span class="line"><span class="comment">//正则字面量:</span></span><br><span class="line"><span class="comment">//由于.是元字符,我们要用\.来代替.</span></span><br><span class="line"><span class="comment">//而\表示转义,要用\\来代替\</span></span><br><span class="line"><span class="keyword">var</span> reg = <span class="regexp">/\./</span>;</span><br><span class="line"><span class="built_in">console</span>.log(reg.test(<span class="string">"adsC.dSf l"</span>));<span class="comment">//true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//正则构造函数:</span></span><br><span class="line"><span class="comment">//由于函数的参数为字符串,而在字符串中\代表转义字符</span></span><br><span class="line"><span class="comment">//故在字面量中的一个\要用\\来代替</span></span><br><span class="line"><span class="keyword">var</span> reg1 = <span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">"\\."</span>);<span class="comment">//等价于reg</span></span><br><span class="line"><span class="built_in">console</span>.log(reg1);<span class="comment">//输出:/\./</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//查找字符串是否含单词child</span></span><br><span class="line">reg = <span class="regexp">/\bchild\b/</span>;</span><br><span class="line"><span class="built_in">console</span>.log(reg.test(<span class="string">"hello children"</span>));<span class="comment">//false</span></span><br><span class="line"><span class="built_in">console</span>.log(reg.test(<span class="string">"hello child ren"</span>));<span class="comment">//true</span></span><br></pre></td></tr></table></figure>
<h3 id="练习-A-去除字符串的前后空格">练习: A.去除字符串的前后空格</h3>
<p>匹配开头和结尾空格的正则表达式: <font style='color:red;font-size:1.2em;font-weight:bold;'> /^\s*|\s*$/g</font></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//可以考虑replace用空串替换空格</span></span><br><span class="line"><span class="keyword">var</span> str = <span class="string">" Aaron Bob "</span>;</span><br><span class="line"><span class="comment">//str = str.replace(/\s/g,"");//去除所有空格</span></span><br><span class="line"><span class="comment">//str = str.replace(/^\s*/,"");//去除开头的空格</span></span><br><span class="line"><span class="comment">//str = str.replace(/\s*$/,"");//去除结尾的空格</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//综合一下:</span></span><br><span class="line">str = str.replace(<span class="regexp">/^\s*|\s*$/g</span>,<span class="string">""</span>);</span><br><span class="line"><span class="comment">//因为是或的关系,若不加全局匹配,方法匹配完开头的空格就返回了</span></span><br><span class="line"><span class="built_in">console</span>.log(str);</span><br></pre></td></tr></table></figure>
<h3 id="练习-B-email的正则表达式">练习: B.email的正则表达式</h3>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*电子邮件格式:</span></span><br><span class="line"><span class="comment"> * hello .hello @ abc .com .cn</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * 任意字母数字下划线 .任意字母数字下划线 @ 任意字母数字 .任意字母(2-5位) .任意字母(2-5位)</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * \w{3,} (\.\w+)* @ [A-z0-9]+ (\.[A-z]{2,5}){1,2}</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">var</span> emailReg = <span class="regexp">/^\w{3,}(\.\w+)*@[A-z0-9]+(\.[A-z]{2,5}){1,2}$/</span>;</span><br><span class="line"><span class="keyword">var</span> email = <span class="string">"[email protected]"</span>;</span><br><span class="line"><span class="built_in">console</span>.log(emailReg.test(email));<span class="comment">//true</span></span><br></pre></td></tr></table></figure>
<br>
<section class="widget copyright desktop mobile">
<div class='content'>
<blockquote>
<p>博客内容遵循 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议</p>
<p>本文永久链接是:<a href=https://crainyday.gitee.io/JS2-5.html>https://crainyday.gitee.io/JS2-5.html</a></p>
</blockquote>
</div>
</section>
<section class="widget qrcode desktop mobile">
<div class='content article-entry'>
<fancybox><img src='https://cdn.jsdelivr.net/gh/crainyday/blog@master/alipay.png'
height='64px'
></fancybox>
<fancybox><img src='https://cdn.jsdelivr.net/gh/crainyday/blog@master/wechat.png'
height='64px'
></fancybox>
</div>
</section>
</div>
<section class='meta' id="footer-meta">
<div class='new-meta-box'>
<div class="new-meta-item date" itemprop="dateUpdated" datetime="2020-03-12T15:01:15+08:00">
<a class='notlink'>
<i class="fas fa-save" aria-hidden="true"></i>
<p>更新于:2020年3月12日</p>
</a>
</div>
<div class="new-meta-item meta-tags"><a class="tag" href="/tags/JS/" rel="nofollow"><i class="fas fa-hashtag" aria-hidden="true"></i><p>JS</p></a></div>
<div class="new-meta-item wordcount">
<a class='notlink'>
<i class="fas fa-keyboard" aria-hidden="true"></i>
<p>字数:2.2k字</p>
</a>
</div>
<div class="new-meta-item readtime">
<a class='notlink'>
<i class="fas fa-hourglass-half" aria-hidden="true"></i>
<p>时长:8分钟</p>
</a>
</div>
<div class="new-meta-item share -mob-share-list">
<div class="-mob-share-list share-body">
<a class="-mob-share-qq" title="" rel="external nofollow noopener noreferrer"
href="http://connect.qq.com/widget/shareqq/index.html?url=https://crainyday.gitee.io/JS2-5.html&title=JS正则 - CRainyDay&pics=https://cdn.jsdelivr.net/gh/crainyday/blog@master/imgs/more/more01.png&summary=JS中的正则"
>
<img src="https://cdn.jsdelivr.net/gh/crainyday/blog@master/lazyload.svg" data-original="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-assets/logo/128/qq.png">
</a>
<a class="-mob-share-qzone" title="" rel="external nofollow noopener noreferrer"
href="https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=https://crainyday.gitee.io/JS2-5.html&title=JS正则 - CRainyDay&pics=https://cdn.jsdelivr.net/gh/crainyday/blog@master/imgs/more/more01.png&summary=JS中的正则"
>
<img src="https://cdn.jsdelivr.net/gh/crainyday/blog@master/lazyload.svg" data-original="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-assets/logo/128/qzone.png">
</a>
<a class="-mob-share-weibo" title="" rel="external nofollow noopener noreferrer"
href="http://service.weibo.com/share/share.php?url=https://crainyday.gitee.io/JS2-5.html&title=JS正则 - CRainyDay&pics=https://cdn.jsdelivr.net/gh/crainyday/blog@master/imgs/more/more01.png&summary=JS中的正则"
>
<img src="https://cdn.jsdelivr.net/gh/crainyday/blog@master/lazyload.svg" data-original="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-assets/logo/128/weibo.png">
</a>
<div class='hoverbox'>
<a><img src="https://cdn.jsdelivr.net/gh/crainyday/blog@master/lazyload.svg" data-original="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-assets/logo/128/wechat.png"></a>
<div class='target'>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPgAAAD4CAAAAADkunTXAAACTElEQVR42u3aQXICMQwEQP7/6eQeilppZBKibd+WImu1cxkkP75uuh7g4ODg4OCL4I9wVf/+5/eeCim+71i94ODg4ODgC+HlAFAEdd/3qtCr/dv1goODg4ODL4ZPg0g1SKQHMa0XHBwcHBwcvP6cBplugAIHBwcHBwfPA0waTKqDBnBwcHBwcPDzA4XpBYFTB/LrkxRwcHBwcPAPgk8v+P3288fcbAQHBwcHB/9D+HSlDYPuIOFYveDg4ODg4IvgaYGnL/qlgantAAcHBwcHXwzvNu6nweNxaB2bpICDg4ODg/9jeBpc0gZF9QC70Jf7gIODg4ODL4JXgWlDoNrgSAcMcYABBwcHBwdfDE/B6aC/us844ICDg4ODgy+CTxv23YZGuWEQBpvLIAYODg4ODr4YHjfqD13E614IBAcHBwcHB7/esDsoSA8s/YeUPwcHBwcHB18E7zbw04bDdL/uxUNwcHBwcPA7wruBJg0wpxoO7feBg4ODg4Mvgnd/6Hcb+WnjP12XHnBwcHBw8EXwUwV1GwLpgU0bHODg4ODg4Bvh04bBFNJtMMQHBg4ODg4OvhB+6od/Wti7AtDT+8DBwcHBwRfBpwGhO3Dofl7dr1wHODg4ODj4QvjpwHIqGHUDzOXn4ODg4ODgi+HdwNK9WJAedBqoygEGHBwcHBz8hvD0+d2DiJf7g4ODg4ODg7e/nw4OpgMKcHBwcHDwO8G7A4XuYKG63+kgBQ4ODg4OvhneXdNGw/Qgq4MNcHBwcHDwzfC7LXBwcHBw8AXrG4wwwYxuZclQAAAAAElFTkSuQmCC">
</div>
</div>
</div>
</div>
</div>
</section>
<div class="prev-next">
<a class='prev' href='/JSDOM1-0.html'>
<p class='title'><i class="fas fa-chevron-left" aria-hidden="true"></i>JSDOM简介</p>
<p class='content'>JS中的宿主对象, JSDOM
一.DOM简介
1.什么是DOM
DOM, 即Document Object Model 文档对象模型
JS中通过操作DOM来对HTML文档进行操作.只要了...</p>
</a>
<a class='next' href='/JS2-4.html'>
<p class='title'>JS内建对象<i class="fas fa-chevron-right" aria-hidden="true"></i></p>
<p class='content'>JS中的一些其他内建对象
一.Date
1.Date对象的创建
在JS中用Date对象来表示一个时间
Date的无参构造函数会封装当前代码执行时的时间.
以字符串为参数的构造函数, 可以指定...</p>
</a>
</div>
</section>
</article>
<section class="widget related_posts shadow floatable desktop mobile">
<header>
<i class="fas fa-bookmark fa-fw" aria-hidden="true"></i><span class='name'>相关文章</span>
</header>
<div class="content">
<ul class="popular-posts"><li class="popular-posts-item"><div class="popular-posts-title"><h3><a href="/JS_tools_01.html" title="JS中UTF-8与汉字互转" rel="bookmark">JS中UTF-8与汉字互转</a></h3></div></li><li class="popular-posts-item"><div class="popular-posts-title"><h3><a href="/Vue3-1.html" title="JS数组函数" rel="bookmark">JS数组函数</a></h3></div></li><li class="popular-posts-item"><div class="popular-posts-title"><h3><a href="/Vue1-1.html" title="Vue的计算属性与监视" rel="bookmark">Vue的计算属性与监视</a></h3></div></li><li class="popular-posts-item"><div class="popular-posts-title"><h3><a href="/Vue1-0.html" title="Vue初识" rel="bookmark">Vue初识</a></h3></div></li><li class="popular-posts-item"><div class="popular-posts-title"><h3><a href="/JS-JSON.html" title="JS-JSON" rel="bookmark">JS-JSON</a></h3></div></li></ul>
</div>
</section>
<!-- 显示推荐文章和评论 -->
<article class="post white-box comments shadow floatable">
<section class="article typo">
<p ct><i class='fas fa-comments'></i> 评论</p>
<section id="comments">
<div id="valine_container" class="valine_thread">
<i class="fas fa-spinner fa-spin fa-fw"></i>
</div>
</section>
</section>
</article>