Skip to content
此页目录
本文总阅读量

JavaScript 常用正则

是否为数字

ts
export const isNumber = (val: string | number) => /^[0-9]*$/.test(val + "")

是否为 n 位数字

ts
export const isNumberLength = (val: string | number, n: number) =>
  new RegExp(`^\\d{${n}}$`).test(val + "")

是否为 n 位数字

ts
export const isAtLeastLength = (val: string | number, n: number) =>
  new RegExp(`^\\d{${n},}$`).test(val + "")

是否为 m 至 n 位数字

ts
export const isIntervalLength = (val: string | number, m: number, n: number) =>
  new RegExp(`^\\d{${m},${n}}$`).test(val + "")

零和非零开头的数字

ts
export const isZeroAndNonZero = (val: string | number) =>
  /^(0|[1-9][0-9]*)$/.test(val + "")

非零开头的最多带两位小数的数字

ts
export const isNonZeroAndTwoDecimalPlaces = (val: string | number) =>
  /^([1-9][0-9]*)+(.[0-9]{1,2})?$/.test(val + "")

带 1-2 位小数的正数或负数

ts
export const isNumWith1_2 = (val: string | number) =>
  /^(\-)?\d+(\.\d{1,2})?$/.test(val + "")

正数、负数、和小数

ts
export const isPositiveAndNegativeDecimals = (val: string | number) =>
  /^(\-|\+)?\d+(\.\d+)?$/.test(val + "")

有两位小数的正实数

ts
export const isTwoDecimalRealNumbers = (val: string | number) =>
  /^[0-9]+(.[0-9]{2})?$/.test(val + "")

有两位小数的正实数

ts
export const is1_3DecimalRealNumbers = (val: string | number) =>
  /^[0-9]+(.[0-9]{1,3})?$/.test(val + "")

非零的正整数

ts
export const isNonZeroPositiveInteger = (val: string | number) =>
  /^[1-9]\d*$/.test(val + "")

非零的负整数

ts
export const isNonZeroNegativeInteger = (val: string | number) =>
  /^-[1-9]\d*$/.test(val + "")

非负整数

ts
export const isNonNegativeInteger = (val: string | number) =>
  /^\d+$/.test(val + "")

非正整数

ts
export const isNonPositiveInteger = (val: string | number) =>
  /^-[1-9]\d*|0$/.test(val + "")

非负浮点数

ts
export const isNonNegativeFloat = (val: string | number) =>
  /^\d+(\.\d+)?$/.test(val + "")

非正浮点数

ts
export const isNonPositiveFloat = (val: string | number) =>
  /^((-\d+(\.\d+)?)|(0+(\.0+)?))$/.test(val + "")

正浮点数

ts
export const isPositiveFloat = (val: string | number) =>
  /^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$/.test(val + "")

负浮点数

ts
export const isNegativeFloat = (val: string | number) =>
  /^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$/.test(val + "")

浮点数

ts
export const isFloat = (val: string | number) =>
  /^(-?\d+)(\.\d+)?$/.test(val + "")

中文汉字,至少输入一个

ts
export const isChinese = (val: string) => /^[\u4e00-\u9fa5]{0,}$/.test(val)

英文和数字

ts
export const isEnglishAndNumbers = (val: string | number) =>
  /^[A-Za-z0-9]+$/.test(val + "")

长度为 3-20 的所有字符

ts
export const is3_20Length = (val: string | number) => /^.{3,20}$/.test(val + "")

是英文字母

ts
export const isEnglish = (val: string) => /^[A-Za-z]+$/.test(val)

是大写英文字母

ts
export const isUppercaseEnglish = (val: string) => /^[A-Z]+$/.test(val)

是小写英文字母

ts
export const isLowercaseEnglish = (val: string) => /^[a-z]+$/.test(val)

数字和英文字母

ts
export const isNumbersAndEnglish = (val: string) => /^[A-Za-z0-9]+$/.test(val)

数字,字母,下划线

ts
export const isNumbersAndEnglishAndUnderscore = (val: string) =>
  /^\w+$/.test(val)

中文、英文、数字包括下划线

ts
export const isNumEngUnderChinese = (val: string) =>
  /^[\u4E00-\u9FA5A-Za-z0-9_]+$/.test(val)

中文、英文、数字但不包括下划线等符号

ts
export const isNumEngChinese = (val: string) =>
  /^[\u4E00-\u9FA5A-Za-z0-9]+$/.test(val)

输入含有^%&',;=?$"等字符

ts
export const isSpecialCharacters = (val: string) => /[%&',;=?$\x22]+/.test(val)

输入含有~的字符

ts
export const isWavyLine = (val: string) => /[~\x22]+/.test(val)

Email 邮箱地址

ts
export const isEmail = (val: string) =>
  /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(val)

URL

ts
export const isUrl = (val: string) => /[a-zA-z]+:\/\/[^\s]*/.test(val)

手机号码

ts
export const isPhone = (val: string | number) =>
  /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(
    val + ""
  )

国内电话号码

(0511-4405222、021-87888822)

ts
export const isDomesticCalls = (val: string | number) =>
  /\d{3}-\d{8}|\d{4}-\d{7}/.test(val + "")

二代身份证

ts
export const isIDCard2 = (val: string | number) =>
  /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(
    val + ""
  )

身份证(15 位、18 位数字)

ts
export const isIDCardNum = (val: string | number) =>
  /^\d{15}|\d{18}$/.test(val + "")

短身份证号码(数字、字母 x 结尾)

ts
export const isIDCard = (val: string) => /^([0-9]){7,18}(x|X)?$/.test(val)

帐号是否合法(字母开头,允许 5-16 字节,允许字母数字下划线)

ts
export const isAccountIsLegal = (val: string) =>
  /^[a-zA-Z][a-zA-Z0-9_]{4,15}$/.test(val)

密码(以字母开头,长度在 6~18 之间,只能包含字母、数字和下划线)

ts
export const isPassword = (val: string) => /^[a-zA-Z]\w{5,17}$/.test(val)

强密码(必须包含大小写字母和数字的组合,不能使用特殊字符,长度在 8-10 之间)

ts
export const isStrongPassword = (val: string, m = 8, n = 10) =>
  new RegExp(`^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{${m},${n}}$`).test(val)

日期格式 2022-10-12 带有 -

ts
export const isDateFormat = (val: string) => /^\d{4}-\d{1,2}-\d{1,2}/.test(val)

一年的 12 个月(01 ~ 09 和 1 ~ 12)

ts
export const isMonth = (val: string) => /^(0?[1-9]|1[0-2])$/.test(val)

一个月的 31 天(01 ~ 09 和 1 ~ 31)

ts
export const isDay = (val: string) =>
  /^((0?[1-9])|((1|2)[0-9])|30|31)$/.test(val)

可以接受四种钱的表示:"10000.00"、"10,000.00"、"10000"、"10,000"

ts
export const isMoney = (val: string) =>
  /^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(.[0-9]{1,2})?$/.test(val)

中国邮政编码

ts
export const isZipCode = (val: string | number) =>
  /[1-9]\d{5}(?!\d)/.test(val + "")

IP 地址

ts
export const isIP = (val: string) =>
  /((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))/.test(
    val
  )

XML 文件

ts
export const isXML = (val: string) =>
  /^([a-zA-Z]+-?)+[a-zA-Z0-9]+\\.[x|X][m|M][l|L]$/.test(val)

中文字符

ts
export const isChineseChar = (val: string) => /[\u4e00-\u9fa5]/.test(val)

首尾包含空白字符(包括空格、制表符、换页符等等)

ts
export const isSpaces = (val: string) => /^\s*|\s*$/.test(val)

腾讯 QQ 号(从 10000 开始)

ts
export const isQQ = (val: string | number) => /[1-9][0-9]{4,}/.test(val + "")

评论

交流群