JavaScript 常用正则示例

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

原文地址:https://www.cnblogs.com/cb-bin/p/8474640.html

时间: 2024-07-30 17:40:47

JavaScript 常用正则示例的相关文章

javascript 常用正则

var pattern = new RegExp("正则表达式"); pattern.text("字符串A");//如果 "字符串A" 中含有与 "正则表达式" 匹配的文本,则返回 true,否则返回 false. 1.章节号: /^([1-9]\d*)(\.[1-9]\d*)*$/://例如:2.2.3.1.3.5.1.10.3 /^[A-Z]{1}(\.[1-9]\d*)+$/;//例如:A.2.3.A.10.6 //未完

JavaScript 常用正则(邮箱、手机号、qq号等)

验证手机号码 // 根据号码段的不同和前缀不同可适当修改 var reValue = /^(0|86|17951)?(13[0-9]|15[012356789]|18[0-9]|14[57]|17[0-9])[0-9]{8}$/; if(!reValue.test(phone)){ return false; }else{ return true; } } 验证邮箱 function isEmail(email){ // @符号前后直接相邻不能为 . var reEmail = /^\w+((-

Javascript 常用函数【3】

jquery选中radio //如果之前有选中的,则把选中radio取消掉 $("#tj_cat .pro_category").each(function() { if ($(this).attr('checked')){ $(this).attr('checked' ,false); } }); //获取被选中的标签的值 radio:(checkbox和这个一样) var val=$('input:radio[name="sex"]:checked').val(

javascript常用代码大全

jquery选中radio //如果之前有选中的,则把选中radio取消掉 $("#tj_cat .pro_category").each(function() { if ($(this).attr('checked')){ $(this).attr('checked' ,false); } }); //获取被选中的标签的值 radio:(checkbox和这个一样) var val=$('input:radio[name="sex"]:checked').val(

JavaScript 的正则也有单行模式了

正则表达式最早是由 Ken Thompson 于 1970 年在他改进过的 QED 编辑器里实现的,正则里最简单的元字符 “.” 在当时所匹配的就是除换行符外的任意字符: "." is a regular expression which matches any character except <nl>. 上面这句话出自 QED 在 1970 年的官方文档,这可能是史上第一份正则文档. 为什么要这么规定?是因为 QED 是以行为单位来编辑文件的,而且行尾的换行符也算在这一

javascript常用经典算法实例详解

javascript常用经典算法实例详解 这篇文章主要介绍了javascript常用算法,结合实例形式较为详细的分析总结了JavaScript中常见的各种排序算法以及堆.栈.链表等数据结构的相关实现与使用技巧,需要的朋友可以参考下 本文实例讲述了javascript常用算法.分享给大家供大家参考,具体如下: 入门级算法-线性查找-时间复杂度O(n)--相当于算法界中的HelloWorld ? 1 2 3 4 5 6 7 8 9 10 //线性搜索(入门HelloWorld) //A为数组,x为要

使用prototype扩展的JavaScript常用函数库

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 /** * 检索数组元素(原型扩展或重载) * @param {o} 被检索的元素值 * @type int * @returns 元素索引 */ Array.prototype.contains = function(o) { var index = -1; for(var i=0;i<thi

jQuery常用正则验证

jQuery常用正则验证  [转] 基础知识:JS中创建正则对象的方式:-使用RegExp()构造函数方式,如:var patten=new RegExp("s$");-使用正则直接量方式,如:var patten=/s$/;注意:就像字符串直接量被定义为包含在引号("")内的字符一样,正则表达式直接量也被定义为包含在一对斜杠(/)之间的字符:斜杠外末尾可以加正则全局标识符. 验证文字输入个数<script type="text/javascript

js 常用正则

2016-09-09 整数或者小数: ^[0-9]+\.{0,1}[0-9]{0,2}$ 只能输入数字: "^[0-9]*$" . 只能输入 n 位的数字: "^\d{n}$" . 只能输入至少 n 位的数字: "^\d{n,}$" . 只能输入 m~n 位的数字:. "^\d{m,n}$" 只能输入零和非零开头的数字: "^(0|[1-9][0-9]*)$" . 只能输入有两位小数的正实数: "