正則表達式語法和使用(全宇宙最全)

1.正則表達式基本語法

-->

function regx(r,s)

{

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

return false;

}

var patrn= new RegExp(r);

if (patrn.exec(s))

return true

return false

}

-->

規(guī)則表達式?:? (填寫/ /之間的表達式)


校驗字符串?:?

4.正則表達式應(yīng)用

"^\d+$"  //非負整數(shù)(正整數(shù) + 0)

"^[0-9]*[1-9][0-9]*$"  //正整數(shù)

"^((-\d+)|(0+))$"  //非正整數(shù)(負整數(shù) + 0)

"^-[0-9]*[1-9][0-9]*$"  //負整數(shù)

"^-?\d+$"    //整數(shù)

"^\d+(\.\d+)?$"  //非負浮點數(shù)(正浮點數(shù) + 0)

"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //正浮點數(shù)

"^((-\d+(\.\d+)?)|(0+(\.0+)?))$"  //非正浮點數(shù)(負浮點數(shù) + 0)

"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //負浮點數(shù)

"^(-?\d+)(\.\d+)?$"  //浮點數(shù)

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

"^[A-Z]+$"  //由26個英文字母的大寫組成的字符串

"^[a-z]+$"  //由26個英文字母的小寫組成的字符串

"^[A-Za-z0-9]+$"  //由數(shù)字和26個英文字母組成的字符串

"^\w+$"  //由數(shù)字、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正則表達式檢驗

//校驗是否全由數(shù)字組成

function isDigit(s)

{

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

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

return true

}

//校驗登錄名:只能輸入5-20個以字母開頭象对、可帶數(shù)字惫撰、“_”开财、“.”的字串

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個字母晴玖、數(shù)字炬守、下劃線

function isPasswd(s)

{

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

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

return true

}

//校驗普通電話眠菇、傳真號碼:可以“+”開頭,除數(shù)字外慈缔,可含有“-”

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

}

//校驗手機號碼:必須以數(shù)字開頭,除數(shù)字外种玛,可含有“-”

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

}

//校驗搜索關(guān)鍵字

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+$"  //非負整數(shù)(正整數(shù) + 0)

"^[0-9]*[1-9][0-9]*$"  //正整數(shù)

"^((-\\d+)|(0+))$"  //非正整數(shù)(負整數(shù) + 0)

"^-[0-9]*[1-9][0-9]*$"  //負整數(shù)

"^-?\\d+$"    //整數(shù)

"^\\d+(\\.\\d+)?$"  //非負浮點數(shù)(正浮點數(shù) + 0)

"^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //正浮點數(shù)

"^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$"  //非正浮點數(shù)(負浮點數(shù) + 0)

"^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //負浮點數(shù)

"^(-?\\d+)(\\.\\d+)?$"  //浮點數(shù)

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

"^[A-Z]+$"  //由26個英文字母的大寫組成的字符串

"^[a-z]+$"  //由26個英文字母的小寫組成的字符串

"^[A-Za-z0-9]+$"  //由數(shù)字和26個英文字母組成的字符串

"^\\w+$"  //由數(shù)字藐鹤、26個英文字母或者下劃線組成的字符串

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

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末瓤檐,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子娱节,更是在濱河造成了極大的恐慌挠蛉,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件肄满,死亡現(xiàn)場離奇詭異谴古,居然都是意外死亡,警方通過查閱死者的電腦和手機稠歉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門掰担,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人怒炸,你說我怎么就攤上這事带饱。” “怎么了阅羹?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵勺疼,是天一觀的道長。 經(jīng)常有香客問我捏鱼,道長执庐,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任导梆,我火速辦了婚禮耕肩,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘问潭。我一直安慰自己猿诸,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布狡忙。 她就那樣靜靜地躺著梳虽,像睡著了一般。 火紅的嫁衣襯著肌膚如雪灾茁。 梳的紋絲不亂的頭發(fā)上窜觉,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天,我揣著相機與錄音北专,去河邊找鬼禀挫。 笑死,一個胖子當(dāng)著我的面吹牛拓颓,可吹牛的內(nèi)容都是我干的语婴。 我是一名探鬼主播,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼砰左!你這毒婦竟也來了匿醒?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤缠导,失蹤者是張志新(化名)和其女友劉穎廉羔,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體僻造,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡憋他,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了髓削。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片竹挡。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖蔬螟,靈堂內(nèi)的尸體忽然破棺而出此迅,到底是詐尸還是另有隱情,我是刑警寧澤旧巾,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布耸序,位于F島的核電站,受9級特大地震影響鲁猩,放射性物質(zhì)發(fā)生泄漏坎怪。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一廓握、第九天 我趴在偏房一處隱蔽的房頂上張望搅窿。 院中可真熱鬧,春花似錦隙券、人聲如沸男应。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽沐飘。三九已至违帆,卻和暖如春呈队,著一層夾襖步出監(jiān)牢的瞬間印荔,已是汗流浹背客税。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留麻蹋,地道東北人宛徊。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓歧杏,卻偏偏與公主長得像陪每,于是被迫代替她去往敵國和親影晓。 傳聞我的和親對象是個殘疾皇子镰吵,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 正則表達式基本語法 1.正則表達式基本語法兩個特殊的符號'^'和'$'。他們的作用是分別指出一個字符串的開始和結(jié)束...
    爵笙彥閱讀 333評論 0 0
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理俯艰,服務(wù)發(fā)現(xiàn)捡遍,斷路器锌订,智...
    卡卡羅2017閱讀 134,672評論 18 139
  • 單例模式 適用場景:可能會在場景中使用到對象竹握,但只有一個實例,加載時并不主動創(chuàng)建辆飘,需要時才創(chuàng)建 最常見的單例模式啦辐,...
    Obeing閱讀 2,073評論 1 10
  • var regexEnum = { intege : "^-?[1-9]\\d*$", // 整數(shù) intege1...
    nick2046閱讀 308評論 0 2
  • 去年,我參加了 發(fā)射 Nexus的5倍和6 p。 谷歌還介紹了像素C,Chromecast刷新, 對于科技最大的公...
    每日一看閱讀 212評論 0 0