Browse Source

增加正则通用函数

duyong mac 7 months ago
parent
commit
df63b32f12
1 changed files with 17 additions and 0 deletions
  1. 17 0
      regular/regular.go

+ 17 - 0
regular/regular.go

@@ -0,0 +1,17 @@
+package regular
+
+import "regexp"
+
+// MaskPhoneNumber 影藏手机号中间位数
+func MaskPhoneNumber(phoneNumber string) string {
+	// 使用正则表达式匹配手机号的前三位和后四位,并将中间四位替换为 *
+	re := regexp.MustCompile(`(\d{3})\d{4}(\d{4})`)
+	return re.ReplaceAllString(phoneNumber, "$1****$2")
+}
+
+// MaskIDNumber 影藏身份证号中间位数
+func MaskIDNumber(idNumber string) string {
+	// 使用正则表达式匹配身份证号的前三位和后四位,并将中间的字符替换为 *
+	re := regexp.MustCompile(`(\d{3})\d+(\d{4})`)
+	return re.ReplaceAllString(idNumber, "$1********$2")
+}