-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathregex-cheatsheet.html
More file actions
575 lines (534 loc) · 19.9 KB
/
regex-cheatsheet.html
File metadata and controls
575 lines (534 loc) · 19.9 KB
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
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>正则表达式速查 - WebUtils</title>
<!-- SEO Meta Tags -->
<meta name="description" content="正则表达式语法速查表" />
<meta name="keywords" content="正则 regex 速查 cheatsheet 语法" />
<meta name="author" content="WebUtils" />
<meta name="robots" content="index, follow" />
<link rel="canonical" href="https://tools.realtime-ai.chat/tools/dev/regex-cheatsheet.html" />
<!-- Open Graph -->
<meta property="og:title" content="正则表达式速查 - WebUtils" />
<meta property="og:description" content="正则表达式语法速查表" />
<meta property="og:type" content="website" />
<meta
property="og:url"
content="https://tools.realtime-ai.chat/tools/dev/regex-cheatsheet.html"
/>
<meta property="og:site_name" content="WebUtils" />
<meta property="og:locale" content="zh_CN" />
<!-- Twitter Card -->
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="正则表达式速查 - WebUtils" />
<meta name="twitter:description" content="正则表达式语法速查表" />
<!-- OG Image -->
<meta property="og:image" content="https://tools.realtime-ai.chat/social-preview.png" />
<meta property="og:image:width" content="1280" />
<meta property="og:image:height" content="640" />
<meta property="og:image:type" content="image/png" />
<meta name="twitter:image" content="https://tools.realtime-ai.chat/social-preview.png" />
<style>
:root {
--bg-deep: #0a0a0f;
--bg-surface: #12121a;
--bg-card: #1a1a24;
--bg-input: #0e0e14;
--text-primary: #e8e8ed;
--text-secondary: #8888a0;
--text-muted: #55556a;
--border-subtle: #2a2a3a;
--border-strong: #3a3a4a;
--accent-cyan: #00f5d4;
--accent-magenta: #f72585;
--accent-green: #10b981;
--accent-red: #f43f5e;
--accent-yellow: #fbbf24;
--accent-blue: #4cc9f0;
--accent-purple: #7b2cbf;
--radius-sm: 4px;
--radius-md: 8px;
--radius-lg: 12px;
--font-sans: "Space Grotesk", -apple-system, blinkmacsystemfont, "Segoe UI", roboto, sans-serif;
--font-mono: "JetBrains Mono", "Courier New", monospace;
--shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}
[data-theme="light"] {
--bg-deep: #fafafa;
--bg-surface: #fff;
--bg-card: #fff;
--bg-input: #f5f5f5;
--text-primary: #1a1a1a;
--text-secondary: #666;
--text-muted: #999;
--border-subtle: #e5e5e5;
--border-strong: #d5d5d5;
--accent-cyan: #00d4b8;
--accent-magenta: #e63975;
--shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}
:root {
--bg: #f8f9fa;
--card-bg: #fff;
--text: #212529;
--color-text-muted: #6c757d;
--border: #dee2e6;
--primary: #0d6efd;
--success: #198754;
--shadow: 0 2px 8px rgb(0, 0, 0, 0.08);
--radius: 8px;
--code-bg: #f1f3f5;
}
.theme-toggle {
position: fixed;
top: 1rem;
right: 1rem;
width: 40px;
height: 40px;
border-radius: 50%;
border: 1px solid var(--border-subtle);
background: var(--bg-card);
cursor: pointer;
font-size: 1.2rem;
z-index: 100;
transition: all 0.2s;
}
.theme-toggle:hover {
transform: scale(1.1);
}
* {
box-sizing: border-box;
}
body {
font-family:
system-ui,
-apple-system,
sans-serif;
background: var(--bg-deep);
color: var(--text-primary);
margin: 0;
padding: 24px;
min-height: 100vh;
}
.container {
max-width: 1000px;
margin: 0 auto;
}
.back-link {
display: inline-block;
margin-bottom: 16px;
color: var(--accent-cyan);
text-decoration: none;
font-size: 0.9rem;
}
h1 {
font-size: 1.75rem;
margin: 0 0 8px;
}
.desc {
color: var(--text-muted);
margin: 0 0 24px;
font-size: 0.95rem;
}
.search-box {
margin-bottom: 20px;
}
.search-box input {
width: 100%;
padding: 12px 16px;
font-size: 1rem;
border: 1px solid var(--border-subtle);
border-radius: var(--radius-md);
background: var(--bg-card);
color: var(--text-primary);
}
.search-box input:focus {
outline: none;
border-color: var(--accent-cyan);
box-shadow: 0 0 0 3px rgb(13, 110, 253, 0.15);
}
.tabs {
display: flex;
gap: 8px;
flex-wrap: wrap;
margin-bottom: 20px;
}
.tab {
padding: 8px 16px;
font-size: 0.9rem;
border: 1px solid var(--border-subtle);
border-radius: 20px;
background: var(--bg-card);
color: var(--text-primary);
cursor: pointer;
}
.tab:hover,
.tab.active {
border-color: var(--accent-cyan);
color: var(--accent-cyan);
}
.tab.active {
background: var(--accent-cyan);
color: #fff;
}
.card {
background: var(--bg-card);
border: 1px solid var(--border-subtle);
border-radius: var(--radius-md);
margin-bottom: 12px;
overflow: hidden;
}
.card-header {
padding: 12px 16px;
background: var(--code-bg);
border-bottom: 1px solid var(--border-subtle);
display: flex;
justify-content: space-between;
align-items: center;
}
.card-header code {
font-family: ui-monospace, monospace;
font-size: 1rem;
color: var(--accent-cyan);
}
.card-header .copy-btn {
background: none;
border: none;
cursor: pointer;
font-size: 0.9rem;
opacity: 0.6;
}
.card-header .copy-btn:hover {
opacity: 1;
}
.card-body {
padding: 12px 16px;
}
.card-body .desc-text {
font-size: 0.9rem;
margin-bottom: 8px;
}
.card-body .example {
font-size: 0.85rem;
color: var(--text-muted);
font-family: ui-monospace, monospace;
}
.category-title {
font-size: 1.2rem;
margin: 24px 0 12px;
padding-bottom: 8px;
border-bottom: 2px solid var(--accent-cyan);
}
.common-patterns {
margin-bottom: 24px;
}
.common-patterns h3 {
font-size: 1rem;
margin: 0 0 12px;
color: var(--text-muted);
}
.pattern-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 8px;
}
.pattern-item {
padding: 8px 12px;
background: var(--bg-card);
border: 1px solid var(--border-subtle);
border-radius: 4px;
cursor: pointer;
font-family: ui-monospace, monospace;
font-size: 0.85rem;
}
.pattern-item:hover {
border-color: var(--accent-cyan);
}
.pattern-item .name {
font-size: 0.8rem;
color: var(--text-muted);
margin-bottom: 4px;
font-family: system-ui, sans-serif;
}
footer {
text-align: center;
margin-top: 32px;
padding-top: 16px;
border-top: 1px solid var(--border-subtle);
color: var(--text-muted);
font-size: 0.85rem;
}
footer a {
color: var(--accent-cyan);
text-decoration: none;
}
.breadcrumb {
background: rgba(255, 255, 255, 0.95);
padding: 8px 15px;
border-radius: 20px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
font-size: 0.85rem;
}
.breadcrumb a {
color: #667eea;
text-decoration: none;
}
.breadcrumb a:hover {
text-decoration: underline;
}
.breadcrumb span {
color: #999;
margin: 0 5px;
}
</style>
<!-- Google Fonts -->
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;500;600;700&family=Space+Grotesk:wght@400;500;600;700&display=swap"
rel="stylesheet"
/>
</head>
<body>
<nav class="breadcrumb">
<a href="../../index.html">首页</a>
<span>›</span>
正则表达式速查
</nav>
<div class="container">
<button class="theme-toggle" onclick="toggleTheme()" title="切换主题">☀️</button>
<h1>正则表达式速查</h1>
<p class="desc">常用正则表达式语法速查,点击即可复制</p>
<div class="common-patterns">
<h3>常用正则模式(点击复制)</h3>
<div class="pattern-grid" id="commonPatterns"></div>
</div>
<div class="search-box">
<input type="text" id="search" placeholder="搜索正则语法..." />
</div>
<div class="tabs" id="tabs"></div>
<div id="content"></div>
<footer>
<a
href="https://github.com/chicogong/html-tools/blob/main/tools/dev/regex-cheatsheet.html"
target="_blank"
>
View source
</a>
· 纯前端工具 · 无服务器 ·
<a href="../../index.html">WebUtils</a>
</footer>
</div>
<script>
const COMMON_PATTERNS = [
{ name: "邮箱", pattern: "^[\\w.-]+@[\\w.-]+\\.\\w+$" },
{ name: "手机号(中国)", pattern: "^1[3-9]\\d{9}$" },
{ name: "URL", pattern: "^https?://[\\w.-]+(?:/[\\w.-]*)*/?$" },
{
name: "IPv4",
pattern:
"^(?:(?:25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$"
},
{
name: "日期(YYYY-MM-DD)",
pattern: "^\\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\\d|3[01])$"
},
{ name: "时间(HH:mm:ss)", pattern: "^(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d$" },
{ name: "十六进制颜色", pattern: "^#?([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$" },
{ name: "身份证号", pattern: "^\\d{17}[\\dXx]$" },
{ name: "中文字符", pattern: "[\\u4e00-\\u9fa5]" },
{ name: "密码强度", pattern: "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).{8,}$" },
{ name: "整数", pattern: "^-?\\d+$" },
{ name: "浮点数", pattern: "^-?\\d+\\.\\d+$" }
];
const REGEX_SYNTAX = {
字符匹配: [
{ syntax: ".", desc: "匹配除换行符外的任意字符", example: "a.c → abc, aXc, a1c" },
{ syntax: "\\d", desc: "匹配数字 [0-9]", example: "\\d\\d → 12, 99" },
{ syntax: "\\D", desc: "匹配非数字", example: "\\D → a, @, 空格" },
{ syntax: "\\w", desc: "匹配单词字符 [a-zA-Z0-9_]", example: "\\w+ → hello, test_1" },
{ syntax: "\\W", desc: "匹配非单词字符", example: "\\W → @, #, 空格" },
{ syntax: "\\s", desc: "匹配空白字符", example: "a\\sb → a b, a\\tb" },
{ syntax: "\\S", desc: "匹配非空白字符", example: "\\S+ → hello" },
{ syntax: "[abc]", desc: "匹配字符集中的任一字符", example: "[aeiou] → 匹配元音" },
{ syntax: "[^abc]", desc: "匹配不在字符集中的字符", example: "[^0-9] → 匹配非数字" },
{ syntax: "[a-z]", desc: "匹配范围内的字符", example: "[a-zA-Z] → 匹配字母" }
],
量词: [
{ syntax: "*", desc: "匹配 0 次或多次", example: "ab*c → ac, abc, abbc" },
{ syntax: "+", desc: "匹配 1 次或多次", example: "ab+c → abc, abbc" },
{ syntax: "?", desc: "匹配 0 次或 1 次", example: "colou?r → color, colour" },
{ syntax: "{n}", desc: "精确匹配 n 次", example: "a{3} → aaa" },
{ syntax: "{n,}", desc: "匹配至少 n 次", example: "a{2,} → aa, aaa, aaaa" },
{ syntax: "{n,m}", desc: "匹配 n 到 m 次", example: "a{2,4} → aa, aaa, aaaa" },
{ syntax: "*?", desc: "非贪婪匹配(最少)", example: "<.*?> → <tag>" },
{ syntax: "+?", desc: "非贪婪匹配 1 次或多次", example: "\\d+? → 匹配单个数字" }
],
锚点: [
{ syntax: "^", desc: "匹配字符串开头", example: "^hello → 开头的hello" },
{ syntax: "$", desc: "匹配字符串结尾", example: "world$ → 结尾的world" },
{ syntax: "\\b", desc: "匹配单词边界", example: "\\bcat\\b → cat(整词)" },
{ syntax: "\\B", desc: "匹配非单词边界", example: "\\Bcat → category中的cat" },
{ syntax: "(?=...)", desc: "正向先行断言", example: "\\d+(?=px) → 100px中的100" },
{ syntax: "(?!...)", desc: "负向先行断言", example: "\\d+(?!px) → 100em中的100" },
{ syntax: "(?<=...)", desc: "正向后行断言", example: "(?<=\\$)\\d+ → $100中的100" },
{ syntax: "(?<!...)", desc: "负向后行断言", example: "(?<!\\$)\\d+ → 非$开头的数字" }
],
分组与引用: [
{ syntax: "(abc)", desc: "捕获分组", example: "(\\d+)-(\\d+) → 捕获两个数字" },
{ syntax: "(?:abc)", desc: "非捕获分组", example: "(?:ab)+ → 不捕获" },
{ syntax: "(?<name>...)", desc: "命名捕获分组", example: "(?<year>\\d{4})" },
{ syntax: "\\1, \\2", desc: "反向引用", example: "(\\w+)\\s\\1 → hello hello" },
{ syntax: "a|b", desc: "或运算", example: "cat|dog → cat 或 dog" }
],
修饰符: [
{ syntax: "i", desc: "忽略大小写", example: "/hello/i → Hello, HELLO" },
{ syntax: "g", desc: "全局匹配", example: "/\\d/g → 匹配所有数字" },
{ syntax: "m", desc: "多行模式", example: "/^hello/m → 每行开头" },
{ syntax: "s", desc: "单行模式(.匹配换行)", example: "/a.b/s → a\\nb" },
{ syntax: "u", desc: "Unicode 模式", example: "/\\u{1F600}/u → 😀" }
],
特殊字符: [
{ syntax: "\\n", desc: "换行符", example: "line1\\nline2" },
{ syntax: "\\r", desc: "回车符", example: "Windows 换行: \\r\\n" },
{ syntax: "\\t", desc: "制表符", example: "col1\\tcol2" },
{ syntax: "\\0", desc: "空字符", example: "字符串结束符" },
{ syntax: "\\\\", desc: "转义反斜杠", example: "C:\\\\Users" },
{ syntax: "\\.", desc: "转义点号", example: "file\\.txt" }
]
};
const categories = Object.keys(REGEX_SYNTAX);
let activeCategory = "全部";
function renderCommonPatterns() {
const container = document.getElementById("commonPatterns");
COMMON_PATTERNS.forEach(function (p) {
const item = document.createElement("div");
item.className = "pattern-item";
item.title = "点击复制";
const name = document.createElement("div");
name.className = "name";
name.textContent = p.name;
item.appendChild(name);
const pattern = document.createElement("div");
pattern.textContent = p.pattern;
item.appendChild(pattern);
item.addEventListener("click", async function () {
try {
await navigator.clipboard.writeText(p.pattern);
item.style.borderColor = "var(--accent-green)";
setTimeout(function () {
item.style.borderColor = "";
}, 500);
} catch (e) {}
});
container.appendChild(item);
});
}
function renderTabs() {
const tabs = document.getElementById("tabs");
tabs.innerHTML = "";
const allTab = document.createElement("button");
allTab.className = "tab" + (activeCategory === "全部" ? " active" : "");
allTab.textContent = "全部";
allTab.addEventListener("click", function () {
setCategory("全部");
});
tabs.appendChild(allTab);
categories.forEach(function (cat) {
const tab = document.createElement("button");
tab.className = "tab" + (activeCategory === cat ? " active" : "");
tab.textContent = cat;
tab.addEventListener("click", function () {
setCategory(cat);
});
tabs.appendChild(tab);
});
}
window.setCategory = function (cat) {
activeCategory = cat;
renderTabs();
renderSyntax();
};
function renderSyntax(filter) {
const content = document.getElementById("content");
content.innerHTML = "";
const search = (filter || "").toLowerCase();
const catsToShow = activeCategory === "全部" ? categories : [activeCategory];
catsToShow.forEach(function (cat) {
const items = REGEX_SYNTAX[cat].filter(function (item) {
if (!search) return true;
return (
item.syntax.toLowerCase().includes(search) ||
item.desc.toLowerCase().includes(search) ||
item.example.toLowerCase().includes(search)
);
});
if (items.length === 0) return;
const title = document.createElement("h2");
title.className = "category-title";
title.textContent = cat;
content.appendChild(title);
items.forEach(function (item) {
const card = document.createElement("div");
card.className = "card";
const header = document.createElement("div");
header.className = "card-header";
const code = document.createElement("code");
code.textContent = item.syntax;
header.appendChild(code);
const copyBtn = document.createElement("button");
copyBtn.className = "copy-btn";
copyBtn.textContent = "📋 复制";
copyBtn.addEventListener("click", async function () {
try {
await navigator.clipboard.writeText(item.syntax);
copyBtn.textContent = "✓ 已复制";
setTimeout(function () {
copyBtn.textContent = "📋 复制";
}, 1000);
} catch (e) {}
});
header.appendChild(copyBtn);
const body = document.createElement("div");
body.className = "card-body";
const desc = document.createElement("div");
desc.className = "desc-text";
desc.textContent = item.desc;
body.appendChild(desc);
const example = document.createElement("div");
example.className = "example";
example.textContent = "示例: " + item.example;
body.appendChild(example);
card.appendChild(header);
card.appendChild(body);
content.appendChild(card);
});
});
}
document.getElementById("search").addEventListener("input", function (e) {
renderSyntax(e.target.value);
});
renderCommonPatterns();
renderTabs();
renderSyntax();
window.toggleTheme = function () {
const html = document.documentElement;
const btn = document.querySelector(".theme-toggle");
const next = html.getAttribute("data-theme") === "dark" ? "" : "dark";
html.setAttribute("data-theme", next);
btn.textContent = next === "dark" ? "🌙" : "☀️";
localStorage.setItem("theme", next || "light");
};
(function () {
const saved = localStorage.getItem("theme");
if (saved === "dark") {
document.documentElement.setAttribute("data-theme", "dark");
const btn = document.querySelector(".theme-toggle");
if (btn) btn.textContent = "🌙";
}
})();
</script>
</body>
</html>