JavaScript 常用正則樣本

來源:互聯網
上載者:User

標籤:號碼   call   +=   複雜   gpo   $2   amp   輸出   密碼   

1. trim功能(清除字串兩端空格)

String.prototype.trim = function() {
 return this.replace(/(^\s+)|(\s+$)/g, ‘‘);

};

‘ abc ‘.trim();
// 輸出 "abc"

 

2. 提取瀏覽器中url的參數名和參數值

function getUrlParam(url) {

  var result = {},

      url = url || window.location.href,

      reg = /([\?|&])(.+?)=([^&?]*)/gi,

      arr = reg.exec(url);

  

  while(arr) {
    result[arr[2]] = arr[3];

    arr = reg.exec(url);

  }

 

  return result;

} 

getUrlParam(‘https://www.baidu.com?a=0&b=1&c=2‘);
// 輸出 "{a: "0", b: "1", c: "2"}"

 

3. 擴充typeof,探明具體類型

function getType(obj) {
 var result = Object.prototype.toString.call(obj);
     result = result.replace(/\[object\s(\w+)\]/, ‘$1‘);

  

  return result;

} 

getType([1,2]);
// 輸出 "Array"
getType(null);
// 輸出 "Null"

 

4. 在字串的指定位置插入新字元

String.prototype.insertAt = function(str, index) {

  index += 1;

  var reg = new RegExp(‘(^.{‘ + index + ‘})‘);

 

  return this.replace(reg, ‘$1‘ + str);

}; 
 
‘abcde‘.insertAt(‘123‘, 2);
// 輸出 "abc123de"

 

5. 隱藏手機號中間4位

function telFormat(tel) {

  tel = String(tel);
 

  return tel.replace(/(\d{3})(\d{4})(\d{4})/, ‘$1****$3‘);

}   

telFormat(13899207998);
// 輸出 "138****7998"

 

6. 字串調換

var name = "Doe Jone";

name.replace(/(\w+)\s* \s*(\w+)/, "$2 $1"); 
// 輸出 "Jone Doe"

 

7. 字串截取

var str = ‘ asfdf === sdfaf ##‘;

str.match(/[^===]+(?=[===])/g);

// 輸出 " asfdf "

 

8. 檢測密碼強度正則

// 必須是包含大小寫字母和數位組合,不能使用特殊字元,長度在8-10之間。

/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$/.test("weeeeeeeW2");
// 輸出 true 

// 密碼強度正則,最少6位,包括至少1個大寫字母,1個小寫字母,1個數字,1個特殊字元

/^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[[email protected]#$%^&*? ]).*$/.test("diaoD123#");

// 輸出 true 

 

9. 檢測中文字元

// 檢測字串是否只含中文

/^[\u4e00-\u9fa5]{0,}$/.test("但是d");
// 輸出 false

/^[\u4e00-\u9fa5]{0,}$/.test("但是");
// 輸出 true

// 包含有中文

/[\u4E00-\u9FA5]/.test("但是d");
// 輸出 true

 

10. 社會安全號碼正則

/^[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("42112319870115371X");

// 輸出 false

 

11. 校正日期 (“yyyy-mm-dd“ 格式,已考慮平閏年)

// 日期正則,簡單判定,未做月份及日期的判定

var dP1 = /^\d{4}(\-)\d{1,2}\1\d{1,2}$/;

console.log(dP1.test("2017-05-11"));

// 輸出 true

console.log(dP1.test("2017-15-11"));
// 輸出 true

// 日期正則,複雜判定

var dP2 = /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/;

console.log(dP2.test("2017-02-11"));
// 輸出 true

console.log(dP2.test("2017-15-11"));

// 輸出 false

console.log(dP2.test("2017-02-29"));

// 輸出 false

 

12. 浮點數正則

// 驗證是否為浮點數

/^(?:[-+])?(?:[0-9]+)?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/.test(0.2);
// 輸出 true 

 

13. Email正則

// 驗證Email
/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test("[email protected]"); 
 

// 1.郵箱以a-z、A-Z、0-9開頭,最小長度為1.

// 2.如果左側部分包含-、_、.則這些特殊符號的前面必須包一位元字或字母。

// [email protected]符號是必填項

// 4.右則部分可分為兩部分,第一部分為郵件供應商網域名稱地址,第二部分為網域名稱尾碼,現已知的最短為2位。

//   最長的為6為。

// 5.郵件供應商域可以包含特殊字元-、_、.

/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test("[email protected]");
// 輸出 true

 

14. 校正傳真號碼

// 國家代碼(2到3位)-區號(2到3位)-電話號碼(7到8位)-分機號(3位)

/^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/.test(‘021-5055455‘);
// 輸出 true

 

15. 手機號正則

/^1[34578]\d{9}$/.test("13611778887");

// 輸出 true

//* 13段:130、131、132、133、134、135、136、137、138、139

//* 14段:145、147

//* 15段:150、151、152、153、155、156、157、158、159

//* 17段:170、176、177、178

//* 18段:180、181、182、183、184、185、186、187、188、189

//* 國際碼 如:中國(+86)

/^((\+?[0-9]{1,4})|(\(\+86\)))?(13[0-9]|14[57]|15[012356789]|17[03678]|18[0-9])\d{8}$/.test("1

// 輸出 true

 

16. URL正則

/^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/.test("http://wangchujiang.com");

// 輸出 true

 

// 擷取url中網域名稱、協議正則 ‘http://xxx.xx/xxx‘,‘https://xxx.xx/xxx‘,‘//xxx.xx/xxx‘

/^(http(?:|s)\:)*\/\/([^\/]+)/.test("http://www.baidu.com");

// 輸出 true

/^((http|https):\/\/(\w+:{0,1}\w*@)?(\S+)|)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/.test(‘https://www.baidu.com/[email protected]#%$^&%$#‘);

// 輸出 true

// 必須有協議 

/^[a-zA-Z]+:\/\//.test("http://www.baidu.com");
// 輸出 true

 

17. RGB Hex顏色正則

/^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/.test("#b8b8b8");

// 輸出 true

 

18. 車牌號正則

/^[京津滬渝冀豫雲遼黑湘皖魯新蘇浙贛鄂桂甘晉蒙陝吉閩貴粵青藏川寧瓊使領A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9掛學警港澳]{1}$/.test("滬B99116");
// 輸出 true

 

19. 校正號

// 6至20位,以字母開頭,字母,數字,減號,底線
/^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/.test("jslite");
// 輸出 true

 

20. 校正QQ號

// 5至11位

/^[1-9][0-9]{4,10}$/.test("398188661");
// 輸出 true

JavaScript 常用正則樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.