正则表达式语法和使用(全宇宙最全)

1.正则表达式基本语法

-->

function regx(r,s)

{

if (r == null || r == ""){

return false;

}

var patrn= new RegExp(r);

if (patrn.exec(s))

return true

return false

}

-->

规则表达式 :  (填写/ /之间的表达式)


校验字符串 : 

4.正則表達式應用

"^\d+$"  //非负整数(正整数 + 0)

"^[0-9]*[1-9][0-9]*$"  //正整数

"^((-\d+)|(0+))$"  //非正整数(负整数 + 0)

"^-[0-9]*[1-9][0-9]*$"  //负整数

"^-?\d+$"    //整数

"^\d+(\.\d+)?$"  //非负浮点数(正浮点数 + 0)

"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //正浮点数

"^((-\d+(\.\d+)?)|(0+(\.0+)?))$"  //非正浮点数(负浮点数 + 0)

"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //负浮点数

"^(-?\d+)(\.\d+)?$"  //浮点数

"^[A-Za-z]+$"  //由26个英文字母组成的字符串

"^[A-Z]+$"  //由26个英文字母的大写组成的字符串

"^[a-z]+$"  //由26个英文字母的小写组成的字符串

"^[A-Za-z0-9]+$"  //由数字和26个英文字母组成的字符串

"^\w+$"  //由数字、26个英文字母或者下划线组成的字符串

"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"    //email地址

"^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$"  //url

/^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$/   //  年-月-日

/^((0([1-9]{1}))|(1[1|2]))/(([0-2]([1-9]{1}))|(3[0|1]))/(d{2}|d{4})$/   // 月/日/年

"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$"   //Emil

"(d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?"     //电话号码

"^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$"   //IP地址

^([0-9A-F]{2})(-[0-9A-F]{2}){5}$   //MAC地址的正则表达式

^[-+]?\d+(\.\d+)?$  //值类型正则表达式

5.javascript正则表达式检验

//校验是否全由数字组成

function isDigit(s)

{

var patrn=/^[0-9]{1,20}$/;

if (!patrn.exec(s)) return false

return true

}

//校验登录名:只能输入5-20个以字母开头、可带数字、“_”、“.”的字串

function isRegisterUserName(s)

{

var patrn=/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/;

if (!patrn.exec(s)) return false

return true

}

//校验用户姓名:只能输入1-30个以字母开头的字串

function isTrueName(s)

{

var patrn=/^[a-zA-Z]{1,30}$/;

if (!patrn.exec(s)) return false

return true

}

//校验密码:只能输入6-20个字母、数字、下划线

function isPasswd(s)

{

var patrn=/^(\w){6,20}$/;

if (!patrn.exec(s)) return false

return true

}

//校验普通电话、传真号码:可以“+”开头,除数字外,可含有“-”

function isTel(s)

{

//var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?(\d){1,12})+$/;

var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;

if (!patrn.exec(s)) return false

return true

}

//校验手机号码:必须以数字开头,除数字外,可含有“-”

function isMobil(s)

{

var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;

if (!patrn.exec(s)) return false

return true

}

//校验邮政编码

function isPostalCode(s)

{

//var patrn=/^[a-zA-Z0-9]{3,12}$/;

var patrn=/^[a-zA-Z0-9 ]{3,12}$/;

if (!patrn.exec(s)) return false

return true

}

//校验搜索关键字

function isSearch(s)

{

var patrn=/^[^`~!@#$%^&*()+=|\\\][\]\{\}:;\'\,.<>/?]{1}[^`~!@$%^&()+=|\\\][\]\{\}:;\'\,.<>?]{0,19}$/;

if (!patrn.exec(s)) return false

return true

}

function isIP(s) //by zergling

{

var patrn=/^[0-9.]{1,20}$/;

if (!patrn.exec(s)) return false

return true

}

/*********************************************************************************

* FUNCTION: isBetween

* PARAMETERS: val AS any value

* lo AS Lower limit to check

* hi AS Higher limit to check

* CALLS: NOTHING

* RETURNS: TRUE if val is between lo and hi both inclusive, otherwise false.

**********************************************************************************/

function isBetween (val, lo, hi) {

if ((val < lo) || (val > hi)) { return(false); }

else { return(true); }

}

/*********************************************************************************

* FUNCTION: isDate checks a valid date

* PARAMETERS: theStr AS String

* CALLS: isBetween, isInt

* RETURNS: TRUE if theStr is a valid date otherwise false.

**********************************************************************************/

function isDate (theStr) {

var the1st = theStr.indexOf('-');

var the2nd = theStr.lastIndexOf('-');

if (the1st == the2nd) { return(false); }

else {

var y = theStr.substring(0,the1st);

var m = theStr.substring(the1st+1,the2nd);

var d = theStr.substring(the2nd+1,theStr.length);

var maxDays = 31;

if (isInt(m)==false || isInt(d)==false || isInt(y)==false) {

return(false); }

else if (y.length < 4) { return(false); }

else if (!isBetween (m, 1, 12)) { return(false); }

else if (m==4 || m==6 || m==9 || m==11) maxDays = 30;

else if (m==2) {

if (y % 4 > 0) maxDays = 28;

else if (y % 100 == 0 && y % 400 > 0) maxDays = 28;

else maxDays = 29;

}

if (isBetween(d, 1, maxDays) == false) { return(false); }

else { return(true); }

}

}

/*********************************************************************************

* FUNCTION: isEuDate checks a valid date in British format

* PARAMETERS: theStr AS String

* CALLS: isBetween, isInt

* RETURNS: TRUE if theStr is a valid date otherwise false.

**********************************************************************************/

function isEuDate (theStr) {

if (isBetween(theStr.length, 8, 10) == false) { return(false); }

else {

var the1st = theStr.indexOf('/');

var the2nd = theStr.lastIndexOf('/');

if (the1st == the2nd) { return(false); }

else {

var m = theStr.substring(the1st+1,the2nd);

var d = theStr.substring(0,the1st);

var y = theStr.substring(the2nd+1,theStr.length);

var maxDays = 31;

if (isInt(m)==false || isInt(d)==false || isInt(y)==false) {

return(false); }

else if (y.length < 4) { return(false); }

else if (isBetween (m, 1, 12) == false) { return(false); }

else if (m==4 || m==6 || m==9 || m==11) maxDays = 30;

else if (m==2) {

if (y % 4 > 0) maxDays = 28;

else if (y % 100 == 0 && y % 400 > 0) maxDays = 28;

else maxDays = 29;

}

if (isBetween(d, 1, maxDays) == false) { return(false); }

else { return(true); }

}

}

}

/********************************************************************************

* FUNCTION: Compare Date! Which is the latest!

* PARAMETERS: lessDate,moreDate AS String

* CALLS: isDate,isBetween

* RETURNS: TRUE if lessDate

*********************************************************************************/

function isComdate (lessDate , moreDate)

{

if (!isDate(lessDate)) { return(false);}

if (!isDate(moreDate)) { return(false);}

var less1st = lessDate.indexOf('-');

var less2nd = lessDate.lastIndexOf('-');

var more1st = moreDate.indexOf('-');

var more2nd = moreDate.lastIndexOf('-');

var lessy = lessDate.substring(0,less1st);

var lessm = lessDate.substring(less1st+1,less2nd);

var lessd = lessDate.substring(less2nd+1,lessDate.length);

var morey = moreDate.substring(0,more1st);

var morem = moreDate.substring(more1st+1,more2nd);

var mored = moreDate.substring(more2nd+1,moreDate.length);

var Date1 = new Date(lessy,lessm,lessd);

var Date2 = new Date(morey,morem,mored);

if (Date1>Date2) { return(false);}

return(true);

}

/*********************************************************************************

* FUNCTION isEmpty checks if the parameter is empty or null

* PARAMETER str AS String

**********************************************************************************/

function isEmpty (str) {

if ((str==null)||(str.length==0)) return true;

else return(false);

}

/*********************************************************************************

* FUNCTION: isInt

* PARAMETER: theStr AS String

* RETURNS: TRUE if the passed parameter is an integer, otherwise FALSE

* CALLS: isDigit

**********************************************************************************/

function isInt (theStr) {

var flag = true;

if (isEmpty(theStr)) { flag=false; }

else

{ for (var i=0; i

if (isDigit(theStr.substring(i,i+1)) == false) {

flag = false; break;

}

}

}

return(flag);

}

/*********************************************************************************

* FUNCTION: isReal

* PARAMETER: heStr AS String

decLen AS Integer (how many digits after period)

* RETURNS: TRUE if theStr is a float, otherwise FALSE

* CALLS: isInt

**********************************************************************************/

function isReal (theStr, decLen) {

var dot1st = theStr.indexOf('.');

var dot2nd = theStr.lastIndexOf('.');

var OK = true;

if (isEmpty(theStr)) return false;

if (dot1st == -1) {

if (!isInt(theStr)) return(false);

else return(true);

}

else if (dot1st != dot2nd) return (false);

else if (dot1st==0) return (false);

else {

var intPart = theStr.substring(0, dot1st);

var decPart = theStr.substring(dot2nd+1);

if (decPart.length > decLen) return(false);

else if (!isInt(intPart) || !isInt(decPart)) return (false);

else if (isEmpty(decPart)) return (false);

else return(true);

}

}

/*********************************************************************************

* FUNCTION: isEmail

* PARAMETER: String (Email Address)

* RETURNS: TRUE if the String is a valid Email address

* FALSE if the passed string is not a valid Email Address

* EMAIL FORMAT:AnyName@EmailServere.g;webmaster@hotmail.com

* @ sign can appear only once in the email address.

*********************************************************************************/

function isEmail (theStr) {

var atIndex = theStr.indexOf('@');

var dotIndex = theStr.indexOf('.', atIndex);

var flag = true;

theSub = theStr.substring(0, dotIndex+1)

if ((atIndex < 1)||(atIndex != theStr.lastIndexOf('@'))||(dotIndex< atIndex + 2)||(theStr.length <= theSub.length))

{ return(false); }

else { return(true); }

}

/*********************************************************************************

* FUNCTION: newWindow

* PARAMETERS: doc -> Document to open in the new window

hite -> Height of the new window

wide -> Width of the new window

bars -> 1-Scroll bars = YES 0-Scroll Bars = NO

resize -> 1-Resizable = YES 0-Resizable = NO

* CALLS: NONE

* RETURNS: New window instance

**********************************************************************************/

function newWindow (doc, hite, wide, bars, resize) {

var winNew="_blank";

var opt="toolbar=0,location=0,directories=0,status=0,menubar=0,";

opt+=("scrollbars="+bars+",");

opt+=("resizable="+resize+",");

opt+=("width="+wide+",");

opt+=("height="+hite);

winHandle=window.open(doc,winNew,opt);

return;

}

/*********************************************************************************

* FUNCTION: DecimalFormat

* PARAMETERS: paramValue -> Field value

* CALLS: NONE

* RETURNS: Formated string

**********************************************************************************/

function DecimalFormat (paramValue) {

var intPart = parseInt(paramValue);

var decPart =parseFloat(paramValue) - intPart;

str = "";

if ((decPart == 0) || (decPart == null)) str += (intPart + ".00");

else str += (intPart + decPart);

return (str);

}

"^\\d+$"  //非负整数(正整数 + 0)

"^[0-9]*[1-9][0-9]*$"  //正整数

"^((-\\d+)|(0+))$"  //非正整数(负整数 + 0)

"^-[0-9]*[1-9][0-9]*$"  //负整数

"^-?\\d+$"    //整数

"^\\d+(\\.\\d+)?$"  //非负浮点数(正浮点数 + 0)

"^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //正浮点数

"^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$"  //非正浮点数(负浮点数 + 0)

"^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //负浮点数

"^(-?\\d+)(\\.\\d+)?$"  //浮点数

"^[A-Za-z]+$"  //由26个英文字母组成的字符串

"^[A-Z]+$"  //由26个英文字母的大写组成的字符串

"^[a-z]+$"  //由26个英文字母的小写组成的字符串

"^[A-Za-z0-9]+$"  //由数字和26个英文字母组成的字符串

"^\\w+$"  //由数字、26个英文字母或者下划线组成的字符串

"^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"    //email地址

"^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$"  //url

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,524评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,869评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,813评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,210评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,085评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,117评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,533评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,219评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,487评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,582评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,362评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,218评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,589评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,899评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,176评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,503评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,707评论 2 335

推荐阅读更多精彩内容

  • 正则表达式基本语法 1.正则表达式基本语法两个特殊的符号'^'和'$'。他们的作用是分别指出一个字符串的开始和结束...
    爵笙彦阅读 324评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,497评论 18 139
  • 单例模式 适用场景:可能会在场景中使用到对象,但只有一个实例,加载时并不主动创建,需要时才创建 最常见的单例模式,...
    Obeing阅读 2,045评论 1 10
  • var regexEnum = { intege : "^-?[1-9]\\d*$", // 整数 intege1...
    nick2046阅读 305评论 0 2
  • 去年,我参加了 发射 Nexus的5倍和6 p。 谷歌还介绍了像素C,Chromecast刷新, 对于科技最大的公...
    每日一看阅读 205评论 0 0