当前位置:网站首页>Regular expression collection
Regular expression collection
2022-07-01 23:45:00 【Wait for the sunset time】
/**
* With 1 start , Followed by 10 digit
*/
export const MOBILE_REGEXP = /1[0-9]{10}/
/**
* 1. User name cannot be a pure number
* 2. By letter 、 Number or underscore 4-16 A composition
*/
export const USER_NAME_REGEXP = /^(?!\d+$)[a-zA-Z0-9_]{4,16}$/
/**
* 1. The password cannot be a pure number
* 2. By letter 、 Number or underscore 4-16 A composition
*/
export const PASSWORD_REGEXP = /^(?!\d+$)([a-zA-Z0-9_]{4,16})/
/**
* Regular mailbox
*/
export const EMAIL_REGEXP = /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/
/**
* Unified social credit code (15/18/20 position , Loose fit )
*/
export const UNIFIED_SOCIAL_CREDIT_CODE = /^(([0-9A-Za-z]{15})|([0-9A-Za-z]{18})|([0-9A-Za-z]{20}))$/
/**
* @param {string} path
* @returns {Boolean}
*/
export function isExternal(path) {
return /^(https?:|mailto:|tel:)/.test(path)
}
/**
* @param {string} str
* @returns {Boolean}
*/
export function validUsername(str) {
return isEmail(str) || isMobile(str) || isUserName(str)
}
/**
* Is it a cell phone number
* @param value The input values
* @return Matching results
*/
export function isMobile(value) {
return isMatching(MOBILE_REGEXP, value)
}
/**
* Is it a user name
* @param value The input values
* @return Matching results
*/
export function isUserName(value) {
return isMatching(USER_NAME_REGEXP, value)
}
/**
* Whether it's a mailbox
* @param value The input values
* @return Matching results
*/
export function isEmail(value) {
return isMatching(EMAIL_REGEXP, value)
}
/**
* Unified social credit code
* @param value The input values
* @returns Matching results
*/
export function isCreditCode(value) {
return isMatching(UNIFIED_SOCIAL_CREDIT_CODE, value)
}
export function isMatching(regexp, value) {
if (!value) {
return false
}
return regexp.test(value)
}
/**
* @param {string} url
* @returns {Boolean}
*/
export function validURL(url) {
const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
return reg.test(url)
}
/**
* @param {string} str
* @returns {Boolean}
*/
export function validLowerCase(str) {
const reg = /^[a-z]+$/
return reg.test(str)
}
/**
* @param {string} str
* @returns {Boolean}
*/
export function validUpperCase(str) {
const reg = /^[A-Z]+$/
return reg.test(str)
}
/**
* @param {string} str
* @returns {Boolean}
*/
export function validAlphabets(str) {
const reg = /^[A-Za-z]+$/
return reg.test(str)
}
/**
* @param {string} email
* @returns {Boolean}
*/
export function validEmail(email) {
const reg = /^(([^<>()\[\]\\.,;:\[email protected]"]+(\.[^<>()\[\]\\.,;:\[email protected]"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
return reg.test(email)
}
/**
* @param {string} str
* @returns {Boolean}
*/
export function isString(str) {
if (typeof str === 'string' || str instanceof String) {
return true
}
return false
}
/**
* @param {Array} arg
* @returns {Boolean}
*/
export function isArray(arg) {
if (typeof Array.isArray === 'undefined') {
return Object.prototype.toString.call(arg) === '[object Array]'
}
return Array.isArray(arg)
}
/**
* password 6-16 position
* @param {Array} password
* @returns {Boolean}
*/
export function validPassword(password) {
return password.length >= 6 && password.length <= 16
}
/**
* Determine whether all spaces As long as there is one other character returned false
* @param {String} str
* @returns {Boolean}
*/
export function validNoEmptySpace(str) {
var reg = /^\s+$/g
return reg.test(str)
}
/**
* Determine whether the rich text is full space
* @param {}} str
* @returns
*/
export function isHtmlNull(str) {
const html = str.replace(/<(?!img).*?>/g, '').replace(/ /gi, '').replace(/(\n)/g, '')
if (html === '') return true
var regu = '^[ ]+$'
var re = new RegExp(regu)
return re.test(html)
}
/**
* Match the spaces on both sides , Match to return true
* @param {String} str
* @returns {Boolean}
*/
export function noSpacesBothSides(str) {
var reg = /(^\s)|(\s$)/g
return reg.test(str)
}
/**
* By letter 、 Numbers 、 Chinese or underlined 4-20 A composition , And cannot be a pure number
*/
/* function userName(value) {
return /^(?!\d+$)[a-zA-Z0-9_\u4e00-\u9fa5]{4,20}$/.test(value);
} */
// The user name has only a length limit , You can type in whatever you like , Cannot be a pure number
function userName(value) {
return /^(?!\d+$).{2,20}$/.test(value);
}
/**
* By letter 、 Numbers 、_、@ Symbol 8-16 A composition , And cannot be a pure number
*/
function password(value) {
return /^(?!\d+$)[[email protected]]{8,16}$/.test(value);
}
/**
* Verify the E-mail format
*/
function email(value) {
return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(value);
}
/**
* Verify phone format
*/
function mobile(value) {
return /^1[3-9]\d{9}$/.test(value)
}
/**
* verification URL Format
*/
function url(value) {
return /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w-.\/?%&=]*)?/.test(value)
}
/**
* Validate date format
*/
function date(value) {
return !/Invalid|NaN/.test(new Date(value).toString())
}
/**
* verification ISO Type of date format
*/
function dateISO(value) {
return /^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/.test(value)
}
/**
* Verify decimal digits
*/
function number(value) {
return /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(value)
}
/**
* Validation of an integer
*/
function digits(value) {
return /^\d+$/.test(value)
}
/**
* Verification of id card number
*/
function idCard(value) {
return /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(
value)
}
/**
* Is the license plate number
*/
function carNo(value) {
// New energy license plate
const xreg = /^[ Beijing, Tianjin, Shanghai, Chongqing, Hebei, Henan, Yunnan, Liaoning, Heilongjiang, Hunan, Anhui, Shandong, new Jiangsu, Zhejiang, Jiangxi, Hubei, Guangxi, Gansu, Shanxi, Inner Mongolia, Shaanxi, Jilin, Fujian, Guizhou, Guangdong, Qinghai Tibet, Sichuan, Ningxia and Hainan A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/;
// Old license plate
const creg = /^[ Beijing, Tianjin, Shanghai, Chongqing, Hebei, Henan, Yunnan, Liaoning, Heilongjiang, Hunan, Anhui, Shandong, new Jiangsu, Zhejiang, Jiangxi, Hubei, Guangxi, Gansu, Shanxi, Inner Mongolia, Shaanxi, Jilin, Fujian, Guizhou, Guangdong, Qinghai Tibet, Sichuan, Ningxia and Hainan A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9 Hang the school police in Hong Kong and Macao ]{1}$/;
if (value.length === 7) {
return creg.test(value);
} else if (value.length === 8) {
return xreg.test(value);
} else {
return false;
}
}
/**
* amount of money , Only 2 Decimal place
*/
function amount(value) {
// amount of money , Only two decimal places are allowed
return /^[1-9]\d*(,\d{3})*(\.\d{1,2})?$|^0\.\d{1,2}$/.test(value);
}
/**
* chinese
*/
function chinese(value) {
let reg = /^[\u4e00-\u9fa5]+$/gi;
return reg.test(value);
}
/**
* Only letters can be entered
*/
function letter(value) {
return /^[a-zA-Z]*$/.test(value);
}
/**
* It can only be letters or numbers
*/
function enOrNum(value) {
// English or numbers
let reg = /^[0-9a-zA-Z]*$/g;
return reg.test(value);
}
/**
* Verify that a value is included
*/
function contains(value, param) {
return value.indexOf(param) >= 0
}
/**
* Validate a value range [min, max]
*/
function range(value, param) {
return value >= param[0] && value <= param[1]
}
/**
* Verify a length range [min, max]
*/
function rangeLength(value, param) {
return value.length >= param[0] && value.length <= param[1]
}
/**
* Is there a fixed line telephone
*/
function landline(value) {
let reg = /^\d{3,4}-\d{7,8}(-\d{3,4})?$/;
return reg.test(value);
}
/**
* Determine whether it is null
*/
function empty(value) {
switch (typeof value) {
case 'undefined':
return true;
case 'string':
if (value.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length == 0) return true;
break;
case 'boolean':
if (!value) return true;
break;
case 'number':
if (0 === value || isNaN(value)) return true;
break;
case 'object':
if (null === value || value.length === 0) return true;
for (var i in value) {
return false;
}
return true;
}
return false;
}
/**
* whether json character string
*/
function jsonString(value) {
if (typeof value == 'string') {
try {
var obj = JSON.parse(value);
if (typeof obj == 'object' && obj) {
return true;
} else {
return false;
}
} catch (e) {
return false;
}
}
return false;
}
/**
* Whether array
*/
function array(value) {
if (typeof Array.isArray === "function") {
return Array.isArray(value);
} else {
return Object.prototype.toString.call(value) === "[object Array]";
}
}
/**
* Whether the object is
*/
function object(value) {
return Object.prototype.toString.call(value) === '[object Object]';
}
/**
* Whether the SMS verification code
*/
function code(value, len = 6) {
return new RegExp(`^\\d{${len}}$`).test(value);
}
export default {
userName,
password,
email,
mobile,
url,
date,
dateISO,
number,
digits,
idCard,
carNo,
amount,
chinese,
letter,
enOrNum,
contains,
range,
rangeLength,
empty,
isEmpty: empty,
jsonString,
landline,
object,
array,
code
}
边栏推荐
- Development trend and future direction of neural network Internet of things
- ADO.NET 之sqlConnection 对象使用摘要
- 有没有一段代码,让你为人类的智慧所折服
- [untitled]
- const // It is a const object... class nullptr_ t
- Chapter 6 data flow modeling
- PyTorch学习记录
- 在代码中使用SqlCommand对象
- Yunxin small class | common cognitive misunderstandings in IM and audio and video
- [LeetCode] 最后一个单词的长度【58】
猜你喜欢
为什么PHP叫超文本预处理器
from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
.env.xxx 文件,加了常量,却undefined
Redis RDB快照
notBlank 和 notEmpty
距离度量 —— 汉明距离(Hamming Distance)
- Oui. Env. Fichier XXX, avec constante, mais non spécifié
What professional classification does the application of Internet of things technology belong to
Using uni simple router, dynamically pass parameters typeerror: cannot convert undefined or null to object
.env.xxx 文件,加了常量,卻undefined
随机推荐
电商RPA机器人,助力品牌电商抢立流量高点
2021 RoboCom 世界机器人开发者大赛-本科组初赛
JPA handwritten SQL, received with user-defined entity classes
问题随记 —— /usr/bin/perl is needed by MySQL-server-5.1.73-1.glibc23.x86_64
Why does blocprovider feel similar to provider?
Depth first search and breadth first search of graph traversal
Development trend and future direction of neural network Internet of things
jpa手写sql,用自定义实体类接收
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
Notes to problems - file /usr/share/mysql/charsets/readme from install of mysql-server-5.1.73-1 glibc23.x86_ 64 c
门级建模—课后习题
Applet form verification encapsulation
【ES实战】ES上的安全性运行方式
Use pair to do unordered_ Key value of map
Matplotlib常用設置
ADO. Net SqlDataAdapter object
2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板
软件架构的本质
力扣今日题-241. 为运算表达式设计优先级
Redis AOF log