mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +03:00
feat(common-regex-memo): add i18n support for memo content (#547)
This commit is contained in:
1 parent
e725356eee
commit
bc12798327
2 files changed
+208
-4
No files matched your search
@@ -1,13 +1,36 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, watch } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { useThemeVars } from 'naive-ui';
|
||||
import Memo from './common-regex.md';
|
||||
import type { Component } from 'vue';
|
||||
|
||||
const themeVars = useThemeVars();
|
||||
const { locale } = useI18n();
|
||||
|
||||
const memoImports = import.meta.glob('./common-regex*.md');
|
||||
const memoComponent = ref<Component | null>(null);
|
||||
|
||||
async function loadMemo(currentLocale = locale.value) {
|
||||
const memoKey = `./common-regex.${currentLocale}.md`;
|
||||
const loader = memoImports[memoKey] ?? memoImports['./common-regex.md'];
|
||||
|
||||
if (!loader) {
|
||||
memoComponent.value = null;
|
||||
return;
|
||||
}
|
||||
|
||||
const module = (await loader()) as { default?: Component };
|
||||
memoComponent.value = module.default ?? module;
|
||||
}
|
||||
|
||||
watch(locale, () => {
|
||||
loadMemo();
|
||||
}, { immediate: true });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<Memo style="overflow-x: auto;" />
|
||||
<component :is="memoComponent" style="overflow-x: auto;" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -17,9 +40,16 @@ const themeVars = useThemeVars();
|
||||
padding: 15px 22px;
|
||||
background-color: v-bind('themeVars.cardColor');
|
||||
border-radius: 4px;
|
||||
white-space: pre-wrap;
|
||||
overflow: auto;
|
||||
}
|
||||
::v-deep(table) {
|
||||
border-collapse: collapse;
|
||||
}
|
||||
::v-deep(table), ::v-deep(td), ::v-deep(th) {
|
||||
border: 1px solid v-bind('themeVars.textColor1');
|
||||
padding: 5px;
|
||||
}
|
||||
::v-deep(a) {
|
||||
color: v-bind('themeVars.textColor3');
|
||||
color: v-bind('themeVars.textColor1');
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,174 @@
|
||||
|
||||
### 常用正则表达式
|
||||
|
||||
#### 1\. 数字
|
||||
|
||||
* [整数](https://www.regexpal.com/?fam=104020) – `/^\d+$/`
|
||||
* [小数](https://www.regexpal.com/?fam=104021) – `/^\d*\.\d+$/`
|
||||
* [整数 + 小数](https://www.regexpal.com/?fam=104022) – `/^\d*(\.\d+)?$/`
|
||||
* [负数、正数的整数 + 小数](https://www.regexpal.com/?fam=104023) – `/^-?\d*(\.\d+)?$/`
|
||||
* [整数 + 小数 + 分数](https://www.regexpal.com/94462) – `/[-]?[0-9]+[,.]?[0-9]*([\/][0-9]+[,.]?[0-9]*)*/`
|
||||
|
||||
#### 2\. 字母数字字符
|
||||
|
||||
* [不含空格的字母数字](https://www.regexpal.com/?fam=104024) – `/^[a-zA-Z0-9]*$/`
|
||||
* [含空格的字母数字](https://www.regexpal.com/?fam=104025) – `/^[a-zA-Z0-9 ]*$/`
|
||||
|
||||
#### 3\. 邮箱
|
||||
|
||||
* [常见邮箱地址](https://www.regexpal.com/?fam=104026) – `/^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})*$/`
|
||||
* [不常见邮箱地址](https://www.regexpal.com/?fam=104027) – `/^([a-z0-9_\.\+-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/`
|
||||
|
||||
#### 4\. 密码强度
|
||||
|
||||
* [复杂](https://www.regexpal.com/?fam=104028):需包含 1 个小写字母、1 个大写字母、1 个数字、1 个特殊字符,且至少 8 个字符长
|
||||
|
||||
/(?=(.\*\[0-9\]))(?=.\*\[\\!@#$%^&\*()\\\\\[\\\]{}\\-\_+=~\`|:;"'<>,./?\])(?=.\*\[a-z\])(?=(.\*\[A-Z\]))(?=(.\*)).{8,}/
|
||||
|
||||
* [中等](https://www.regexpal.com/?fam=104029):需包含 1 个小写字母、1 个大写字母、1 个数字,且至少 8 个字符长
|
||||
|
||||
/(?=(.\*\[0-9\]))((?=.\*\[A-Za-z0-9\])(?=.\*\[A-Z\])(?=.\*\[a-z\]))^.{8,}$/
|
||||
|
||||
#### 5\. 用户名
|
||||
|
||||
* [字母数字字符串](https://www.regexpal.com/?fam=104030),可包含 \_ 和 –,长度为 3 到 16 个字符 – `/^[a-z0-9_-]{3,16}$/`
|
||||
|
||||
#### 6\. URL
|
||||
|
||||
* 包含 [http(s) 协议](https://www.regexpal.com/?fam=104034)
|
||||
|
||||
```
|
||||
/https?:\\/\\/(www\\.)?\[-a-zA-Z0-9@:%.\_\\+~#=\]{2,256}\\.\[a-z\]{2,6}\\b(\[-a-zA-Z0-9@:%\_\\+.~#()?&//=\]\*)/
|
||||
```
|
||||
|
||||
* [协议可选](https://www.regexpal.com/?fam=104035)
|
||||
|
||||
```
|
||||
/(https?:\\/\\/)?(www\\.)?\[-a-zA-Z0-9@:%.\_\\+~#=\]{2,256}\\.\[a-z\]{2,6}\\b(\[-a-zA-Z0-9@:%\_\\+.~#?&//=\]\*)/
|
||||
```
|
||||
|
||||
#### 7\. IP 地址
|
||||
|
||||
* [IPv4 地址](https://www.regexpal.com/?fam=104036)
|
||||
```
|
||||
/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/
|
||||
```
|
||||
* [IPv6 地址](https://www.regexpal.com/?fam=104037)
|
||||
```
|
||||
/(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/
|
||||
```
|
||||
|
||||
* 同时匹配 [IPv4、IPv6 地址](https://www.regexpal.com/?fam=104038)
|
||||
```
|
||||
/((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))/
|
||||
```
|
||||
|
||||
#### 8\. 日期
|
||||
|
||||
* 日期格式 [YYYY-MM-dd](https://www.regexpal.com/?fam=104039),使用分隔符 `-`
|
||||
`/([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/`
|
||||
* 日期格式 [dd-MM-YYYY](https://regexr.com/?346hf),使用分隔符 `-` 或 `.` 或 `/`
|
||||
`/^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/`
|
||||
* 日期格式 [dd-mmm-YYYY](https://regexr.com/39tr1),使用分隔符 `-` 或 `.` 或 `/`
|
||||
`/^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]|(?:Jan|Mar|May|Jul|Aug|Oct|Dec)))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)(?:0?2|(?:Feb))\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/`
|
||||
|
||||
#### 9\. 时间
|
||||
|
||||
* 时间格式 [HH:MM 12 小时制](https://www.regexpal.com/?fam=104040),前导 0 可选
|
||||
|
||||
`/^(0?[1-9]|1[0-2]):[0-5][0-9]$/`
|
||||
|
||||
* 时间格式 HH:MM 12 小时制,前导 0 可选,**[带 AM/PM 标识](https://www.regexpal.com/?fam=104041)**
|
||||
|
||||
`/((1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]))/`
|
||||
|
||||
* 时间格式 [HH:MM 24 小时制](https://www.regexpal.com/?fam=104042),带前导 0
|
||||
|
||||
`/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/`
|
||||
|
||||
* 时间格式 [HH:MM 24 小时制,前导 0 可选](https://www.regexpal.com/?fam=104043)
|
||||
|
||||
`/^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/`
|
||||
|
||||
* 时间格式 [HH:MM:SS 24 小时制](https://www.regexpal.com/?fam=104044)
|
||||
|
||||
`/(?:[01]\d|2[0123]):(?:[012345]\d):(?:[012345]\d)/`
|
||||
|
||||
#### 10\. HTML 标签
|
||||
|
||||
* [带属性的元素](https://www.regexpal.com/95941) `/<\/?[\w\s]*>|<.+[\W]>/`
|
||||
|
||||
#### 11\. JavaScript 处理器
|
||||
|
||||
* [内联 JS 处理器](https://www.regexpal.com/?fam=104055) `/\bon\w+=\S+(?=.*>)/`
|
||||
* [带元素的内联 JS 处理器](https://www.regexpal.com/94641) `/(?:<[^>]+\s)(on\S+)=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?/`
|
||||
|
||||
#### 12\. Slug
|
||||
|
||||
* [Slug](https://www.regexpal.com/?fam=104056) `/^[a-z0-9]+(?:-[a-z0-9]+)*$/`
|
||||
|
||||
#### 13\. 匹配字符串中的重复项
|
||||
|
||||
* [搜索重复项](https://www.regexpal.com/?fam=104060) `/(\b\w+\b)(?=.*\b\1\b)/`
|
||||
|
||||
#### 14\. 电话号码
|
||||
|
||||
* [国际电话号码](https://www.regexpal.com/?fam=99127) – 带可选的国家代码/分机号
|
||||
```
|
||||
`/* International Phone Numbers */`
|
||||
/^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:``#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$/
|
||||
```
|
||||
|
||||
_注意:_ **仅**在没有其他选择时才使用正则表达式验证电话号码。有一些[现成的库](https://stackoverflow.com/a/15644461/4717533)可以更准确地处理电话号码,应优先使用这些库。
|
||||
|
||||
#### 15\. 文件路径
|
||||
|
||||
* [带文件名和扩展名的文件路径](https://www.regexpal.com/?fam=104047)
|
||||
|
||||
```
|
||||
/((\\/|\\\\|\\/\\/|https?:\\\\\\\\|https?:\\/\\/)\[a-z0-9 \_@\\-^!#$%&+={}.\\/\\\\\\\[\\\]\]+)+\\.\[a-z\]+$/
|
||||
```
|
||||
|
||||
* 带可选文件名、扩展名的文件路径
|
||||
|
||||
```
|
||||
/^(.+)/(\[^/\]+)$/
|
||||
```
|
||||
|
||||
* [带 3 字符扩展名的文件名](https://www.regexpal.com/?fam=104048)
|
||||
|
||||
```
|
||||
/^\[\\w,\\s-\]+\\.\[A-Za-z\]{3}$/
|
||||
```
|
||||
|
||||
### 其他正则表达式
|
||||
|
||||
#### 1\. 邮政编码
|
||||
|
||||
**没有**单一的正则表达式可以处理所有邮政编码,因为世界各地的邮政编码并不遵循统一的模式。这里有一个[列表](https://stackoverflow.com/a/7185241/4717533),包含各个国家/地区特定的正则表达式。
|
||||
|
||||
#### 2\. 支付验证
|
||||
|
||||
这里有一个[链接](https://www.regular-expressions.info/creditcard.html),包含用于验证 Visa、Mastercard 等主流信用卡的正则表达式。
|
||||
|
||||
#### 3\. 身份证件
|
||||
|
||||
* 社会保障号 – [参考](https://www.codeproject.com/Articles/651609/Validating-Social-Security-Numbers-through-Regular)
|
||||
|
||||
```
|
||||
/\* can use either hypen(-) or space( ) character as separator \*/
|
||||
|
||||
/^((?!219-09-9999|078-05-1120)(?!666|000|9\\d{2})\\d{3}-(?!00)\\d{2}-(?!0{4})\\d{4})|((?!219 09 9999|078 05 1120)(?!666|000|9\\d{2})\\d{3} (?!00)\\d{2} (?!0{4})\\d{4})|((?!219099999|078051120)(?!666|000|9\\d{2})\\d{3}(?!00)\\d{2}(?!0{4})\\d{4})$/
|
||||
```
|
||||
|
||||
* 护照 – `/^[A-PR-WY][1-9]\d\s?\d{4}[1-9]$/`
|
||||
|
||||
#### 参考文献
|
||||
* [https://www.codeproject.com/validate-passport-number](https://www.codeproject.com/Questions/1046445/How-to-validate-passport-number-using-javascript)
|
||||
* [https://gist.github.com/](https://gist.github.com/nerdsrescueme/1237767)
|
||||
* [https://code.tutsplus.com/regular-expressions](https://code.tutsplus.com/tutorials/8-regular-expressions-you-should-know--net-6149)
|
||||
* [https://www.regular-expressions.info](https://www.regular-expressions.info/)
|
||||
* [http://www.regexlib.com/](http://www.regexlib.com/)
|
||||
* [https://projects.lukehaas.me/regexhub/](https://projects.lukehaas.me/regexhub/)
|
||||
* [http://wiki.zoolz.com/commonly-used-regular-expressions/](http://wiki.zoolz.com/commonly-used-regular-expressions/)
|
||||
* [https://www.smashingmagazine.com/advanced-regular-expressions/](https://www.smashingmagazine.com/2009/05/introduction-to-advanced-regular-expressions/)
|
||||
Reference in new issue
Block a user