regexpCommon 是一个jQuery插件,提供了一些常用的正则表达式:主要包括:整数、小数、email地址、URL地址、电话号码、IP地址、十六进制数、邮编、国家、货币、MAC地址等。(作者好久不更新,看看即可)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
(function($) { $.regexpCommon = function(regexpDesc) { return $.regexpCommon.regexpPattern[regexpDesc].call(); }; $.regexpCommon.regexpPattern = { // numbers numberInteger : function() { return /^[-+]?[1-9]\d*\.?[0]*$/; }, numberFloat : function() { return /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/; }, // email email : function() { return /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/; }, ssn : function() { return /^\d{3}-\d{2}-\d{4}$/; }, url : function() { return /^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$/; }, phoneNumberUS : function() { return /^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$/; }, zipCodeUS : function() { return /^(\d{5}-\d{4}|\d{5}|\d{9})$|^([a-zA-Z]\d[a-zA-Z] \d[a-zA-Z]\d)$/; }, currencyUS : function() { return /^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$/; }, htmlHexCode : function() { return /^#([a-fA-F0-9]){3}(([a-fA-F0-9]){3})?$/; }, dottedQuadIP : function() { return /^(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4] \d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4] \d|25[0-5])$/; }, macAddress : function() { return /^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$/; } }; }) (jQuery); |
jQuery plugin to provide common regular expressions
jquery.regexpCommon.js provides commonly used regular expressions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
someVar.match($.regexpCommon('numberInteger')); someVar.match($.regexpCommon('numberFloat')); someVar.match($.regexpCommon('email')); someVar.match($.regexpCommon('ssn')); someVar.match($.regexpCommon('url')); someVar.match($.regexpCommon('phoneNumberUS')); someVar.match($.regexpCommon('zipCodeUS')); someVar.match($.regexpCommon('currencyUS')); someVar.match($.regexpCommon('htmlHexCode')); someVar.match($.regexpCommon('dottedQuadIP')); someVar.match($.regexpCommon('macAddress')); Example: var quadIP = '127.0.0.1'; if (quadIP.match($.regexpCommon('dottedQuadIP')) ) { message += quadIP + " is a Dotted Quad IP Address"; } |
官方网站下载地址:https://code.google.com/archive/p/regexpcommon/downloads
转载请注明:PHP笔记 » jQuery正则表达式插件regexpCommon