Loading... <p style="margin-top:0px;margin-bottom:0px;padding:0px;max-width:100%;clear:both;min-height:1em;white-space:pre-wrap;color:#3E3E3E;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;font-size:16px;line-height:25.6px;box-sizing:border-box !important;background-color:#FFFFFF;"> <span style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;font-size:14px;">正则表达式——古老而又强大的文本处理工具。仅用一段简短的表达式语句,就能快速地实现一个复杂的业务逻辑。掌握正则表达式,让你的开发效率有一个质的飞跃。</span> </p> <p style="margin-top:0px;margin-bottom:0px;padding:0px;max-width:100%;clear:both;min-height:1em;white-space:pre-wrap;color:#3E3E3E;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;font-size:16px;line-height:25.6px;box-sizing:border-box !important;background-color:#FFFFFF;"> <span style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;font-size:14px;">正则表达式经常被用于字段或任意字符串的校验,比如下面这段校验基本日期格式的JavaScript代码:</span> <span style="line-height:25.6px;margin:0px;padding:0px;max-width:100%;font-size:14px;box-sizing:border-box !important;word-wrap:break-word !important;"> </span><span style="line-height:25.6px;"> </span> </p> <pre class="prettyprint lang-js linenums">var reg = /^(\\d{1,4})(-|\\/)(\\d{1,2})\\2(\\d{1,2})$/; var r = fieldValue.match(reg); if(r==null)alert('Date format error!');</pre> <p style="margin-top:0px;margin-bottom:0px;padding:0px;max-width:100%;clear:both;min-height:1em;white-space:pre-wrap;color:#3E3E3E;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;font-size:16px;line-height:25.6px;box-sizing:border-box !important;background-color:#FFFFFF;"> <span style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;color:#AB1942;"><strong style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;">1、校验密码强度</strong></span><span style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;font-size:14px;"> </span> </p> <p style="margin-top:0px;margin-bottom:0px;padding:0px;max-width:100%;clear:both;min-height:1em;white-space:pre-wrap;color:#3E3E3E;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;font-size:16px;line-height:25.6px;box-sizing:border-box !important;background-color:#FFFFFF;"> <span style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;font-size:14px;">密码的强度必须包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间</span> </p> <pre class="prettyprint linenums">^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$</pre> <h4 style="margin:0px;padding:0px;font-weight:400;font-size:16px;max-width:100%;color:#3E3E3E;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;line-height:25.6px;white-space:normal;box-sizing:border-box !important;word-wrap:break-word !important;background-color:#FFFFFF;"> <strong style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;"><span style="margin:0px;padding:0px;max-width:100%;color:#AB1942;box-sizing:border-box !important;word-wrap:break-word !important;">2、校验中文</span></strong> </h4> <p style="margin-top:0px;margin-bottom:0px;padding:0px;max-width:100%;clear:both;min-height:1em;white-space:pre-wrap;color:#3E3E3E;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;font-size:16px;line-height:25.6px;box-sizing:border-box !important;background-color:#FFFFFF;"> <span style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;font-size:14px;">字符串只能是中文</span> </p> <pre class="prettyprint linenums">^[\\u4e00-\\u9fa5]{0,}$</pre> <p> <strong style="color:#AB1942;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;font-size:16px;line-height:25.6px;margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;background-color:#FFFFFF;">3、由数字,26个英文字母或下划线组成的字符串</strong> </p> <pre class="prettyprint linenums">^\\w+$</pre> <h4 style="margin:0px;padding:0px;font-weight:400;font-size:16px;max-width:100%;color:#3E3E3E;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;line-height:25.6px;white-space:normal;box-sizing:border-box !important;word-wrap:break-word !important;background-color:#FFFFFF;"> <span style="margin:0px;padding:0px;max-width:100%;color:#AB1942;box-sizing:border-box !important;word-wrap:break-word !important;"><strong style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;">4、校验E-Mail 地址</strong></span> </h4> <pre class="prettyprint linenums">[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?</pre> <p style="margin-top:0px;margin-bottom:0px;padding:0px;max-width:100%;clear:both;min-height:1em;white-space:pre-wrap;color:#3E3E3E;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;font-size:16px;line-height:25.6px;box-sizing:border-box !important;background-color:#FFFFFF;"> <strong style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;"><span style="margin:0px;padding:0px;max-width:100%;color:#AB1942;box-sizing:border-box !important;word-wrap:break-word !important;">5、校验身份证号码</span></strong> </p> <p style="margin-top:0px;margin-bottom:0px;padding:0px;max-width:100%;clear:both;min-height:1em;white-space:pre-wrap;color:#3E3E3E;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;font-size:16px;line-height:25.6px;box-sizing:border-box !important;background-color:#FFFFFF;"> <strong style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;"><span style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;font-size:14px;">15位:</span></strong> </p> <pre class="prettyprint linenums">^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$</pre> <strong style="margin:0px;padding:0px;max-width:100%;color:#3E3E3E;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;font-size:16px;line-height:25.6px;white-space:pre-wrap;box-sizing:border-box !important;word-wrap:break-word !important;background-color:#FFFFFF;"><span style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;font-size:14px;">18位:</span></strong> <pre class="prettyprint linenums">^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$</pre> <h4 style="margin:0px;padding:0px;font-weight:400;font-size:16px;max-width:100%;color:#3E3E3E;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;line-height:25.6px;white-space:normal;box-sizing:border-box !important;word-wrap:break-word !important;background-color:#FFFFFF;"> <span style="margin:0px;padding:0px;max-width:100%;color:#AB1942;box-sizing:border-box !important;word-wrap:break-word !important;"><strong style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;">6、校验日期</strong></span> </h4> <p style="margin-top:0px;margin-bottom:0px;padding:0px;max-width:100%;clear:both;min-height:1em;white-space:pre-wrap;color:#3E3E3E;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;font-size:16px;line-height:25.6px;box-sizing:border-box !important;background-color:#FFFFFF;"> <span style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;font-size:14px;">“yyyy-mm-dd“ 格式的日期校验,已考虑平闰年</span> </p> <pre class="prettyprint linenums">^(?:(?!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)$</pre> <p style="margin-top:0px;margin-bottom:0px;padding:0px;max-width:100%;clear:both;min-height:1em;white-space:pre-wrap;color:#3E3E3E;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;font-size:16px;line-height:25.6px;box-sizing:border-box !important;background-color:#FFFFFF;"> <span style="margin:0px;padding:0px;max-width:100%;color:#AB1942;box-sizing:border-box !important;word-wrap:break-word !important;"><strong style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;">7、校验金额</strong></span> </p> <p style="margin-top:0px;margin-bottom:0px;padding:0px;max-width:100%;clear:both;min-height:1em;white-space:pre-wrap;color:#3E3E3E;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;font-size:16px;line-height:25.6px;box-sizing:border-box !important;background-color:#FFFFFF;"> <span style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;font-size:14px;">金额校验,精确到2位小数</span> </p> <pre class="prettyprint linenums">^[0-9]+(.[0-9]{2})?$</pre> <h4 style="margin:0px;padding:0px;font-weight:400;font-size:16px;max-width:100%;color:#3E3E3E;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;line-height:25.6px;white-space:normal;box-sizing:border-box !important;word-wrap:break-word !important;background-color:#FFFFFF;"> <strong style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;"><span style="margin:0px;padding:0px;max-width:100%;color:#AB1942;box-sizing:border-box !important;word-wrap:break-word !important;">8、校验手机号</span></strong> </h4> <p> <strong style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;"><span style="margin:0px;padding:0px;max-width:100%;color:#AB1942;box-sizing:border-box !important;word-wrap:break-word !important;"><span style="color:#3E3E3E;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;font-size:14px;line-height:22.4px;white-space:pre-wrap;background-color:#FFFFFF;">下面是国内 13、15、18开头的手机号正则表达式</span><br /> </span></strong> </p> <pre class="prettyprint linenums">^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$</pre> <strong style="margin:0px;padding:0px;max-width:100%;color:#AB1942;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;font-size:16px;line-height:25.6px;white-space:pre-wrap;box-sizing:border-box !important;word-wrap:break-word !important;background-color:#FFFFFF;"><span style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;">9、判断IE的版本</span></strong> <pre class="prettyprint linenums">^.*MSIE [5-8](?:\\.[0-9]+)?(?!.*Trident\\/[5-9]\\.0).*$</pre> <h4 style="margin:0px;padding:0px;font-weight:400;font-size:16px;max-width:100%;color:#3E3E3E;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;line-height:25.6px;white-space:normal;box-sizing:border-box !important;word-wrap:break-word !important;background-color:#FFFFFF;"> <span style="margin:0px;padding:0px;max-width:100%;color:#AB1942;box-sizing:border-box !important;word-wrap:break-word !important;"><strong style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;">10、校验IP-v4地址</strong></span> </h4> <pre class="prettyprint linenums">\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b</pre> <strong style="margin:0px;padding:0px;max-width:100%;color:#AB1942;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;font-size:16px;line-height:25.6px;white-space:pre-wrap;box-sizing:border-box !important;word-wrap:break-word !important;background-color:#FFFFFF;">11、校验IP-v6地址</strong> <pre class="prettyprint linenums">(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])) </pre> <strong style="margin:0px;padding:0px;max-width:100%;color:#AB1942;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;font-size:16px;line-height:25.6px;white-space:pre-wrap;box-sizing:border-box !important;word-wrap:break-word !important;background-color:#FFFFFF;"><span style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;">12、检查URL的前缀</span></strong> <pre class="prettyprint linenums">if (!s.match(/^[a-zA-Z]+:\\/\\//)){ s = 'http://' + s;}</pre> <h4 style="margin:0px;padding:0px;font-weight:400;font-size:16px;max-width:100%;color:#3E3E3E;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;line-height:25.6px;white-space:normal;box-sizing:border-box !important;word-wrap:break-word !important;background-color:#FFFFFF;"> <span style="margin:0px;padding:0px;max-width:100%;color:#AB1942;box-sizing:border-box !important;word-wrap:break-word !important;"><strong style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;">13、提取URL链接</strong></span> </h4> <pre class="prettyprint linenums">^(f|ht){1}(tp|tps):\\/\\/([\\w-]+\\.)+[\\w-]+(\\/[\\w- ./?%&=]*)?</pre> <strong style="margin:0px;padding:0px;max-width:100%;color:#AB1942;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;font-size:16px;line-height:25.6px;white-space:pre-wrap;box-sizing:border-box !important;word-wrap:break-word !important;background-color:#FFFFFF;"><span style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;">14、文件路径及扩展名校验</span></strong> <pre class="prettyprint linenums">^([a-zA-Z]\\:|\\\\)\\\\([^\\\\]+\\\\)*[^\\/:*?"<>|]+\\.txt(l)?$</pre> <strong style="margin:0px;padding:0px;max-width:100%;color:#AB1942;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;font-size:16px;line-height:25.6px;white-space:pre-wrap;box-sizing:border-box !important;word-wrap:break-word !important;background-color:#FFFFFF;"><span style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;">15、提取Color Hex Codes</span></strong> <pre class="prettyprint linenums">^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$</pre> <h4 style="margin:0px;padding:0px;font-weight:400;font-size:16px;max-width:100%;color:#3E3E3E;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;line-height:25.6px;white-space:normal;box-sizing:border-box !important;word-wrap:break-word !important;background-color:#FFFFFF;"> <span style="margin:0px;padding:0px;max-width:100%;color:#AB1942;box-sizing:border-box !important;word-wrap:break-word !important;"><strong style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;">16、提取网页图片</strong></span> </h4> <pre class="prettyprint linenums">\\< *[img][^\\\\>]*[src] *= *[\\"\\']{0,1}([^\\"\\'\\ >]*)</pre> <h4 style="margin:0px;padding:0px;font-weight:400;font-size:16px;max-width:100%;color:#3E3E3E;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;line-height:25.6px;white-space:normal;box-sizing:border-box !important;word-wrap:break-word !important;background-color:#FFFFFF;"> <span style="margin:0px;padding:0px;max-width:100%;color:#AB1942;box-sizing:border-box !important;word-wrap:break-word !important;"><strong style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;">17、提取页面超链接</strong></span> </h4> <pre class="prettyprint linenums">(<a\\s*(?!.*\\brel=)[^>]*)(href="https?:\\/\\/)((?!(?:(?:www\\.)?'.implode('|(?:www\\.)?', $follow_list).'))[^"]+)"((?!.*\\brel=)[^>]*)(?:[^>]*)></pre> <strong style="margin:0px;padding:0px;max-width:100%;color:#AB1942;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;font-size:16px;line-height:25.6px;white-space:pre-wrap;box-sizing:border-box !important;word-wrap:break-word !important;background-color:#FFFFFF;"><span style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;">18、查找CSS属性</span></strong> <pre class="prettyprint linenums">^\\s*[a-zA-Z\\-]+\\s*[:]{1}\\s[a-zA-Z0-9\\s.#]+[;]{1}</pre> <h4 style="margin:0px;padding:0px;font-weight:400;font-size:16px;max-width:100%;color:#3E3E3E;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;line-height:25.6px;white-space:normal;box-sizing:border-box !important;word-wrap:break-word !important;background-color:#FFFFFF;"> <span style="margin:0px;padding:0px;max-width:100%;color:#AB1942;box-sizing:border-box !important;word-wrap:break-word !important;"><strong style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;">19、抽取注释</strong></span> </h4> <pre class="prettyprint linenums"><!--(.*?)--></pre> <h4 style="margin:0px;padding:0px;font-weight:400;font-size:16px;max-width:100%;color:#3E3E3E;font-family:'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;line-height:25.6px;white-space:normal;box-sizing:border-box !important;word-wrap:break-word !important;background-color:#FFFFFF;"> <span style="margin:0px;padding:0px;max-width:100%;color:#AB1942;box-sizing:border-box !important;word-wrap:break-word !important;"><strong style="margin:0px;padding:0px;max-width:100%;box-sizing:border-box !important;word-wrap:break-word !important;">20、匹配HTML标签</strong></span> </h4> <pre class="prettyprint lang-js linenums"><\\/?\\w+((\\s+\\w+(\\s*=\\s*(?:".*?"|'.*?'|[\\^'">\\s]+))?)+\\s*|\\s*)\\/?></pre> 最后修改:1970 年 01 月 01 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏
此处评论已关闭