当前位置:网站首页>正则表达式收集
正则表达式收集
2022-07-01 23:39:00 【坐等夕阳落time】
/**
* 以1开头,后面跟10位数
*/
export const MOBILE_REGEXP = /1[0-9]{10}/
/**
* 1. 用户名不能为纯数字
* 2. 由字母、数字或下划线 4-16位组成
*/
export const USER_NAME_REGEXP = /^(?!\d+$)[a-zA-Z0-9_]{4,16}$/
/**
* 1. 密码不能为纯数字
* 2. 由字母、数字或下划线 4-16位组成
*/
export const PASSWORD_REGEXP = /^(?!\d+$)([a-zA-Z0-9_]{4,16})/
/**
* 邮箱正则
*/
export const EMAIL_REGEXP = /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/
/**
* 统一社会信用代码(15/18/20位,宽松匹配)
*/
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)
}
/**
* 是否是手机号
* @param value 输入值
* @return 匹配结果
*/
export function isMobile(value) {
return isMatching(MOBILE_REGEXP, value)
}
/**
* 是否是用户名
* @param value 输入值
* @return 匹配结果
*/
export function isUserName(value) {
return isMatching(USER_NAME_REGEXP, value)
}
/**
* 是否是邮箱
* @param value 输入值
* @return 匹配结果
*/
export function isEmail(value) {
return isMatching(EMAIL_REGEXP, value)
}
/**
* 统一社会信用代码
* @param value 输入值
* @returns 匹配结果
*/
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)
}
/**
* 密码 6-16位
* @param {Array} password
* @returns {Boolean}
*/
export function validPassword(password) {
return password.length >= 6 && password.length <= 16
}
/**
* 判断是否全为空格 只要有一个其他字符返回false
* @param {String} str
* @returns {Boolean}
*/
export function validNoEmptySpace(str) {
var reg = /^\s+$/g
return reg.test(str)
}
/**
* 判断富文本是否为全空格
* @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)
}
/**
* 匹配两侧空格,匹配到就返回true
* @param {String} str
* @returns {Boolean}
*/
export function noSpacesBothSides(str) {
var reg = /(^\s)|(\s$)/g
return reg.test(str)
}
/**
* 由字母、数字、中文或下划线 4-20位组成,且不能为纯数字
*/
/* function userName(value) {
return /^(?!\d+$)[a-zA-Z0-9_\u4e00-\u9fa5]{4,20}$/.test(value);
} */
// 用户名只有长度限制,可以随便输入,不能为纯数字
function userName(value) {
return /^(?!\d+$).{2,20}$/.test(value);
}
/**
* 由字母、数字、_、@符号 8-16位组成,且不能为纯数字
*/
function password(value) {
return /^(?!\d+$)[[email protected]]{8,16}$/.test(value);
}
/**
* 验证电子邮箱格式
*/
function email(value) {
return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(value);
}
/**
* 验证手机格式
*/
function mobile(value) {
return /^1[3-9]\d{9}$/.test(value)
}
/**
* 验证URL格式
*/
function url(value) {
return /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w-.\/?%&=]*)?/.test(value)
}
/**
* 验证日期格式
*/
function date(value) {
return !/Invalid|NaN/.test(new Date(value).toString())
}
/**
* 验证ISO类型的日期格式
*/
function dateISO(value) {
return /^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/.test(value)
}
/**
* 验证十进制数字
*/
function number(value) {
return /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(value)
}
/**
* 验证整数
*/
function digits(value) {
return /^\d+$/.test(value)
}
/**
* 验证身份证号码
*/
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)
}
/**
* 是否车牌号
*/
function carNo(value) {
// 新能源车牌
const xreg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/;
// 旧车牌
const creg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
if (value.length === 7) {
return creg.test(value);
} else if (value.length === 8) {
return xreg.test(value);
} else {
return false;
}
}
/**
* 金额,只允许2位小数
*/
function amount(value) {
//金额,只允许保留两位小数
return /^[1-9]\d*(,\d{3})*(\.\d{1,2})?$|^0\.\d{1,2}$/.test(value);
}
/**
* 中文
*/
function chinese(value) {
let reg = /^[\u4e00-\u9fa5]+$/gi;
return reg.test(value);
}
/**
* 只能输入字母
*/
function letter(value) {
return /^[a-zA-Z]*$/.test(value);
}
/**
* 只能是字母或者数字
*/
function enOrNum(value) {
//英文或者数字
let reg = /^[0-9a-zA-Z]*$/g;
return reg.test(value);
}
/**
* 验证是否包含某个值
*/
function contains(value, param) {
return value.indexOf(param) >= 0
}
/**
* 验证一个值范围[min, max]
*/
function range(value, param) {
return value >= param[0] && value <= param[1]
}
/**
* 验证一个长度范围[min, max]
*/
function rangeLength(value, param) {
return value.length >= param[0] && value.length <= param[1]
}
/**
* 是否固定电话
*/
function landline(value) {
let reg = /^\d{3,4}-\d{7,8}(-\d{3,4})?$/;
return reg.test(value);
}
/**
* 判断是否为空
*/
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;
}
/**
* 是否json字符串
*/
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;
}
/**
* 是否数组
*/
function array(value) {
if (typeof Array.isArray === "function") {
return Array.isArray(value);
} else {
return Object.prototype.toString.call(value) === "[object Array]";
}
}
/**
* 是否对象
*/
function object(value) {
return Object.prototype.toString.call(value) === '[object Object]';
}
/**
* 是否短信验证码
*/
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
}
边栏推荐
- 云信小课堂 | IM及音视频中常见的认知误区
- 第六章 数据流建模
- Deep learning | three concepts: epoch, batch, iteration
- What are the common types of points mall games?
- Anomaly-Transformer (ICLR 2022 Spotlight)复现过程及问题
- Linux基础 —— CentOS7 离线安装 MySQL
- Chapter 6 data flow modeling
- Yunxin small class | common cognitive misunderstandings in IM and audio and video
- Postgresql随手记(10)动态执行EXECUTING语法解析过程
- Notes to problems - file /usr/share/mysql/charsets/readme from install of mysql-server-5.1.73-1 glibc23.x86_ 64 c
猜你喜欢

The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem

The essence of software architecture

Practical application and extension of plain framework

Concurrentskiplistmap -- principle of table skipping

Is there a piece of code that makes you convinced by human wisdom

神经网络物联网的发展趋势和未来方向

华为HMS Core携手超图为三维GIS注入新动能

写给当前及未来博士研究生一些建议整理分享

ARP message header format and request flow

问题随记 —— file /usr/share/mysql/charsets/README from install of MySQL-server-5.1.73-1.glibc23.x86_64 c
随机推荐
Similarities and differences between the defined identity execution function authid determiner and PostgreSQL in Oracle
Key points and difficulties of the course "information content security" at Harbin Institute of Technology
[must] bm41 output the right view of the binary tree [medium +]
What are the common types of points mall games?
华为HMS Core携手超图为三维GIS注入新动能
深度学习 | 三个概念:Epoch, Batch, Iteration
Practical application and extension of plain framework
MySQL binlog cleanup
问题随记 —— /usr/bin/perl is needed by MySQL-server-5.1.73-1.glibc23.x86_64
Depth first search and breadth first search of graph traversal
2021 robocom world robot developer competition - preliminary competition of higher vocational group
写给当前及未来博士研究生一些建议整理分享
Daily three questions 6.30 (2)
SWT / anr problem - SWT causes kernel fuse deadlock
Commemorate becoming the first dayus200 tripartite demo contributor
ShanDong Multi-University Training #3
Notblank and notempty
[es practice] safe operation mode on ES
Various global files related to [.Net core] program
常见的积分商城游戏类型有哪些?