-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-8.html
205 lines (204 loc) · 6.62 KB
/
1-8.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
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" href="./public/favicon.ico" />
<meta http-equiv="cache-control" content="no-cache" />
<title></title>
<link rel="stylesheet" href=" https://necolas.github.io/normalize.css/8.0.1/normalize.css" />
<link rel="stylesheet" href="./hightlight/default.min.css" />
<link rel="stylesheet" href="./css/main.css" />
<link rel="stylesheet" href="./css/copybutton.css" />
<link rel="stylesheet" href="./css/hightlight.css" />
<script src="./hightlight/hightlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.11/clipboard.min.js"></script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-BEVZJDBC7Z"></script>
<script src="./js/gtag.js"></script>
</head>
<body>
<header>
<nav>
<h1>
<span id="toggle-menu"></span>
<a href="index.html"></a>
</h1>
</nav>
</header>
<main>
<aside>
<nav></nav>
</aside>
<article>
<h2>1-8 通用正則表達式</h2>
<p>以下是一些軟體業常用的正則表達式範例:</p>
<h3>驗證電子郵件地址:</h3>
<pre><code class="language-js">
const reg = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$/
</code></pre>
<h3>驗證網址:</h3>
<pre><code class="language-js">
try {
new URL(str)
return true
} catch (err) {
return false
}
</code></pre>
<h3>驗證 http 網址:</h3>
<pre><code class="language-js">
try {
const newUrl = new URL(str)
return newUrl.protocol === 'http:' || newUrl.protocol === 'https:'
} catch (err) {
return false
}
</code></pre>
<h3>使用正則驗證網址:</h3>
<pre><code class="language-js">
const pattern = new RegExp(
'^([a-zA-Z]+:\\/\\/)?' + // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR IP (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
'(\\#[-a-z\\d_]*)?$', // fragment locator
'i'
)
pattern.test(str)
</code></pre>
<h3>使用正則驗證 http 網址:</h3>
<pre><code class="language-js">
const pattern = new RegExp(
'^([a-zA-Z]+:\\/\\/)?' + // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR IP (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
'(\\#[-a-z\\d_]*)?$', // fragment locator
'i'
)
pattern.test(str)
</code></pre>
<h3>主流信用卡</h3>
<pre><code class="language-js">
const pattern = /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|622((12[6-9]|1[3-9][0-9])|([2-8][0-9][0-9])|(9(([0-1][0-9])|(2[0-5]))))[0-9]{10}|64[4-9][0-9]{13}|65[0-9]{14}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})*$/
pattern.test(str)
</code></pre>
<h3>美國運通信用卡</h3>
<pre><code class="language-js">
const pattern = /^(3[47][0-9]{13})*$/
pattern.test(str)
</code></pre>
<h3>MasterCard</h3>
<pre><code class="language-js">
const pattern = /^(5[1-5][0-9]{14})*$/
pattern.test(str)
</code></pre>
<h3>Visa 卡</h3>
<pre><code class="language-js">
const pattern = /^(4[0-9]{12}(?:[0-9]{3})?)*$/
pattern.test(str)
</code></pre>
<h3>JCB Card</h3>
<pre><code class="language-js">
const pattern = /^(?:2131|1800|35\d{3})\d{11}$/.test(str)
pattern.test(str)
</code></pre>
<h3>匹配中文字符</h3>
<pre><code class="language-js">
const pattern = /^[\p{Script=Han}]+$/u
pattern.test(str)
</code></pre>
<h3>匹配半形非負整數(包括0)</h3>
<pre><code class="language-js">
const pattern = /^\d+$/
pattern.test(str)
</code></pre>
<h3>匹配半形字母</h3>
<pre><code class="language-js">
const pattern = /^[a-zA-Z]+$/
pattern.test(str)
</code></pre>
<h3>匹配半形小寫字母</h3>
<pre><code class="language-js">
const pattern = /^[a-z]+$/
pattern.test(str)
</code></pre>
<h3>匹配半形大寫字母</h3>
<pre><code class="language-js">
const pattern = /^[A-Z]+$/
pattern.test(str)
</code></pre>
<h3>判斷是否全形英文字母</h3>
<pre><code class="language-js">
const pattern = /[\uFF21-\uFF3A\uFF41-\uFF5A]/
pattern.test(str)
</code></pre>
<h3>匹配半形英文字母和數字</h3>
<pre><code class="language-js">
const pattern = /^[a-zA-Z0-9]+$/
pattern.test(str)
</code></pre>
<h3>匹配半形數字(不含0)</h3>
<pre><code class="language-js">
const pattern = ^[1-9]\d*$
pattern.test(str)
</code></pre>
<h3></h3>
<pre><code class="language-js">
const pattern =
pattern.test(str)
</code></pre>
<h3>匹配半形正浮點數</h3>
<pre><code class="language-js">
const pattern = /^[1-9]\d*.\d+|0.\d*[1-9]\d*$/
pattern.test(str)
</code></pre>
<h3>匹配半形非負浮點數(包括0)</h3>
<pre><code class="language-js">
const pattern = /^\d+(.\d+)?$/
pattern.test(str)
</code></pre>
<h3>判斷是否全形數字</h3>
<pre><code class="language-js">
const pattern = /[\uFF10-\uFF19]/
pattern.test(str)
</code></pre>
<h3>判斷是否全形字元</h3>
<pre><code class="language-js">
const pattern = /[\uFF00-\uFFEF]/
pattern.test(str)
</code></pre>
<h3>判斷是否半形字元</h3>
<pre><code class="language-js">
const pattern = /[\u0020-\u007E]/
pattern.test(str)
</code></pre>
<h3>判斷是否全形標點符號</h3>
<pre><code class="language-js">
const pattern = /[\uFF01-\uFF0F\uFF1A-\uFF20\uFF3B-\uFF40\uFF5B-\uFF65]/
pattern.test(str)
</code></pre>
<h3>判斷是否半形標點符號</h3>
<pre><code class="language-js">
const pattern = /[\u0021-\u002F\u003A-\u0040\u005B-\u0060]/
pattern.test(str)
</code></pre>
<h3>判斷是否全形空白字元</h3>
<pre><code class="language-js">
const pattern = /[\u3000]/
pattern.test(str)
</code></pre>
<h3>判斷是否半形空白字元</h3>
<pre><code class="language-js">
const pattern = /[\u0020]/
pattern.test(str)
</code></pre>
</article>
</main>
</body>
<script type="module" src="./js/main.js"></script>
</html>