Ver Fonte

优化正则

duyong mac há 6 meses atrás
pai
commit
52cb8bffea
1 ficheiros alterados com 6 adições e 0 exclusões
  1. 6 0
      regular/regular.go

+ 6 - 0
regular/regular.go

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