转载

知道这20个正则表达式,能让你少写1,000行代码

文/技匠(简书作者)

原文链接:http://www.jianshu.com/p/e7bb97218946/comments/2135754

著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

正则表达式,一个十分古老而又强大的文本处理工具,仅仅用一段非常简短的表达式语句,便能够快速实现一个非常复杂的业务逻辑。熟练地掌握正则表达式的话,能够使你的开发效率得到极大的提升。

正则表达式经常被用于字段或任意字符串的校验,如下面这段校验基本日期格式的JavaScript代码:

<spanclass="hljs-keyword">var</span> reg = /^(/d{<spanclass="hljs-number">1</span>,<spanclass="hljs-number">4</span>})(-|//)(/d{<spanclass="hljs-number">1</span>,<spanclass="hljs-number">2</span>})/<spanclass="hljs-number">2</span>(/d{<spanclass="hljs-number">1</span>,<spanclass="hljs-number">2</span>})$/;  <spanclass="hljs-keyword">var</span> r = fieldValue.<spanclass="hljs-keyword">match</span>(reg);             <spanclass="hljs-keyword">if</span>(r==<spanclass="hljs-literal">null</span>)alert(<spanclass="hljs-symbol">'Date</span> format error!'); 

下面是 技匠 整理的,在前端开发中经常使用到的 20 个正则表达式。

1 . 校验密码强度

密码的强度必须是包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间。

^(?=.*/d)(?=.*[a-z])(?=.*[A-Z]).{<spanclass="hljs-number">8</span>,<spanclass="hljs-number">10</span>}$ 

2. 校验中文

字符串仅能是中文。

^<spanclass="hljs-special">[</span><spanclass="hljs-command">/u</span>4e00-<spanclass="hljs-command">/u</span>9fa5<spanclass="hljs-special">]</span><spanclass="hljs-special">{</span>0,<spanclass="hljs-special">}</span><spanclass="hljs-formula">$</span> 

3. 由数字、26个英文字母或下划线组成的字符串

^<spanclass="hljs-command">/w</span>+<spanclass="hljs-formula">$</span> 

4. 校验E-Mail 地址

同密码一样,下面是E-mail地址合规性的正则检查语句。

[<spanclass="hljs-link_label">/w!#$%&'*+/=?^_`{|}~-</span>]+(?:/.[<span class="hljs-link_label">/w!#$%&'*+/=?^_`{|}~-</span>]+)<span class="hljs-emphasis">*@(?:[/w](?:[/w-]*</span>[<span class="hljs-link_label">/w</span>])?/.)+[<span class="hljs-link_label">/w</span>](<span class="hljs-link_url">?:[/w-]*[/w]</span>)? 

5. 校验身份证号码

下面是身份证号码的正则校验。15 或 18位。

15位:

^[<spanclass="hljs-number">1</span>-<spanclass="hljs-number">9</span>]/d{<spanclass="hljs-number">7</span>}((<spanclass="hljs-number">0</span>/d)|(<spanclass="hljs-number">1</span>[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">2</span>]))(([<spanclass="hljs-number">0</span>|<spanclass="hljs-number">1</span>|<spanclass="hljs-number">2</span>]/d)|<spanclass="hljs-number">3</span>[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">1</span>])/d{<spanclass="hljs-number">3</span>}$ 

18位:

^[<spanclass="hljs-number">1</span>-<spanclass="hljs-number">9</span>]/d{<spanclass="hljs-number">5</span>}[<spanclass="hljs-number">1</span>-<spanclass="hljs-number">9</span>]/d{<spanclass="hljs-number">3</span>}((<spanclass="hljs-number">0</span>/d)|(<spanclass="hljs-number">1</span>[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">2</span>]))(([<spanclass="hljs-number">0</span>|<spanclass="hljs-number">1</span>|<spanclass="hljs-number">2</span>]/d)|<spanclass="hljs-number">3</span>[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">1</span>])/d{<spanclass="hljs-number">3</span>}([<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>]|X)$ 

6. 校验日期

“yyyy-mm-dd“ 格式的日期校验,已考虑平闰年。

^(?:(?!0000)[<spanclass="hljs-link_label">0-9</span>]{4}-(?:(?:0[<spanclass="hljs-link_label">1-9</span>]|1[<spanclass="hljs-link_label">0-2</span>])-(?:0[<spanclass="hljs-link_label">1-9</span>]|1[<spanclass="hljs-link_label">0-9</span>]|2[<spanclass="hljs-link_label">0-8</span>])|(?:0[<spanclass="hljs-link_label">13-9</span>]|1[<spanclass="hljs-link_label">0-2</span>])-(?:29|30)|(?:0[<spanclass="hljs-link_label">13578</span>]|1[<spanclass="hljs-link_label">02</span>])-31)|(?:[<spanclass="hljs-link_label">0-9</span>]{2}(?:0[<spanclass="hljs-link_label">48</span>]|[<spanclass="hljs-link_label">2468</span>][<spanclass="hljs-link_reference">048</span>]|[<spanclass="hljs-link_label">13579</span>][<spanclass="hljs-link_reference">26</span>])|(?:0[<spanclass="hljs-link_label">48</span>]|[<spanclass="hljs-link_label">2468</span>][<spanclass="hljs-link_reference">048</span>]|[<spanclass="hljs-link_label">13579</span>][<spanclass="hljs-link_reference">26</span>])00)-02-29)$ 

7. 校验金额

金额校验,精确到2位小数。

^[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>]+(.[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>]{<spanclass="hljs-number">2</span>})?$ 

8. 校验手机号

下面是国内 13、15、18开头的手机号正则表达式。(可根据目前国内收集号扩展前两位开头号码)

^(<spanclass="hljs-number">13</span>[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>]|<spanclass="hljs-number">14</span>[<spanclass="hljs-number">5</span>|<spanclass="hljs-number">7</span>]|<spanclass="hljs-number">15</span>[<spanclass="hljs-number">0</span>|<spanclass="hljs-number">1</span>|<spanclass="hljs-number">2</span>|<spanclass="hljs-number">3</span>|<spanclass="hljs-number">5</span>|<spanclass="hljs-number">6</span>|<spanclass="hljs-number">7</span>|<spanclass="hljs-number">8</span>|<spanclass="hljs-number">9</span>]|<spanclass="hljs-number">18</span>[<spanclass="hljs-number">0</span>|<spanclass="hljs-number">1</span>|<spanclass="hljs-number">2</span>|<spanclass="hljs-number">3</span>|<spanclass="hljs-number">5</span>|<spanclass="hljs-number">6</span>|<spanclass="hljs-number">7</span>|<spanclass="hljs-number">8</span>|<spanclass="hljs-number">9</span>])/d{<spanclass="hljs-number">8</span>}$ 

9. 判断IE的版本

IE目前还没被完全取代,很多页面还是需要做版本兼容,下面是IE版本检查的表达式。

^.*MSIE [<spanclass="hljs-number">5</span>-<spanclass="hljs-number">8</span>](?:/.[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>]+)?(?!.*Trident//[<spanclass="hljs-number">5</span>-<spanclass="hljs-number">9</span>]/<spanclass="hljs-number">.0</span>).*$ 

10. 校验IP-v4地址

IP4 正则语句。

/b(?:(?:25[<spanclass="hljs-link_label">0-5</span>]|2[<spanclass="hljs-link_label">0-4</span>][<spanclass="hljs-link_reference">0-9</span>]|[<spanclass="hljs-link_label">01</span>]?[<spanclass="hljs-link_label">0-9</span>][<spanclass="hljs-link_reference">0-9</span>]?)/.){3}(?:25[<spanclass="hljs-link_label">0-5</span>]|2[<spanclass="hljs-link_label">0-4</span>][<spanclass="hljs-link_reference">0-9</span>]|[<spanclass="hljs-link_label">01</span>]?[<spanclass="hljs-link_label">0-9</span>][<spanclass="hljs-link_reference">0-9</span>]?)/b 

11. 校验IP-v6地址

IP6 正则语句。

(([<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>a-fA-F]{<spanclass="hljs-number">1</span>,<spanclass="hljs-number">4</span>}:){<spanclass="hljs-number">7</span>,<spanclass="hljs-number">7</span>}[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>a-fA-F]{<spanclass="hljs-number">1</span>,<spanclass="hljs-number">4</span>}|([<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>a-fA-F]{<spanclass="hljs-number">1</span>,<spanclass="hljs-number">4</span>}:){<spanclass="hljs-number">1</span>,<spanclass="hljs-number">7</span>}:|([<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>a-fA-F]{<spanclass="hljs-number">1</span>,<spanclass="hljs-number">4</span>}:){<spanclass="hljs-number">1</span>,<spanclass="hljs-number">6</span>}:[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>a-fA-F]{<spanclass="hljs-number">1</span>,<spanclass="hljs-number">4</span>}|([<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>a-fA-F]{<spanclass="hljs-number">1</span>,<spanclass="hljs-number">4</span>}:){<spanclass="hljs-number">1</span>,<spanclass="hljs-number">5</span>}(:[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>a-fA-F]{<spanclass="hljs-number">1</span>,<spanclass="hljs-number">4</span>}){<spanclass="hljs-number">1</span>,<spanclass="hljs-number">2</span>}|([<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>a-fA-F]{<spanclass="hljs-number">1</span>,<spanclass="hljs-number">4</span>}:){<spanclass="hljs-number">1</span>,<spanclass="hljs-number">4</span>}(:[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>a-fA-F]{<spanclass="hljs-number">1</span>,<spanclass="hljs-number">4</span>}){<spanclass="hljs-number">1</span>,<spanclass="hljs-number">3</span>}|([<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>a-fA-F]{<spanclass="hljs-number">1</span>,<spanclass="hljs-number">4</span>}:){<spanclass="hljs-number">1</span>,<spanclass="hljs-number">3</span>}(:[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>a-fA-F]{<spanclass="hljs-number">1</span>,<spanclass="hljs-number">4</span>}){<spanclass="hljs-number">1</span>,<spanclass="hljs-number">4</span>}|([<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>a-fA-F]{<spanclass="hljs-number">1</span>,<spanclass="hljs-number">4</span>}:){<spanclass="hljs-number">1</span>,<spanclass="hljs-number">2</span>}(:[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>a-fA-F]{<spanclass="hljs-number">1</span>,<spanclass="hljs-number">4</span>}){<spanclass="hljs-number">1</span>,<spanclass="hljs-number">5</span>}|[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>a-fA-F]{<spanclass="hljs-number">1</span>,<spanclass="hljs-number">4</span>}:((:[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>a-fA-F]{<spanclass="hljs-number">1</span>,<spanclass="hljs-number">4</span>}){<spanclass="hljs-number">1</span>,<spanclass="hljs-number">6</span>})|:((:[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>a-fA-F]{<spanclass="hljs-number">1</span>,<spanclass="hljs-number">4</span>}){<spanclass="hljs-number">1</span>,<spanclass="hljs-number">7</span>}|:)|fe80:(:[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>a-fA-F]{<spanclass="hljs-number">0</span>,<spanclass="hljs-number">4</span>}){<spanclass="hljs-number">0</span>,<spanclass="hljs-number">4</span>}%[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>a-zA-Z]{<spanclass="hljs-number">1</span>,}|::(ffff(:<spanclass="hljs-number">0</span>{<spanclass="hljs-number">1</span>,<spanclass="hljs-number">4</span>}){<spanclass="hljs-number">0</span>,<spanclass="hljs-number">1</span>}:){<spanclass="hljs-number">0</span>,<spanclass="hljs-number">1</span>}((<spanclass="hljs-number">25</span>[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">5</span>]|(<spanclass="hljs-number">2</span>[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">4</span>]|<spanclass="hljs-number">1</span>{<spanclass="hljs-number">0</span>,<spanclass="hljs-number">1</span>}[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>]){<spanclass="hljs-number">0</span>,<spanclass="hljs-number">1</span>}[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>])/.){<spanclass="hljs-number">3</span>,<spanclass="hljs-number">3</span>}(<spanclass="hljs-number">25</span>[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">5</span>]|(<spanclass="hljs-number">2</span>[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">4</span>]|<spanclass="hljs-number">1</span>{<spanclass="hljs-number">0</span>,<spanclass="hljs-number">1</span>}[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>]){<spanclass="hljs-number">0</span>,<spanclass="hljs-number">1</span>}[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>])|([<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>a-fA-F]{<spanclass="hljs-number">1</span>,<spanclass="hljs-number">4</span>}:){<spanclass="hljs-number">1</span>,<spanclass="hljs-number">4</span>}:((<spanclass="hljs-number">25</span>[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">5</span>]|(<spanclass="hljs-number">2</span>[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">4</span>]|<spanclass="hljs-number">1</span>{<spanclass="hljs-number">0</span>,<spanclass="hljs-number">1</span>}[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>]){<spanclass="hljs-number">0</span>,<spanclass="hljs-number">1</span>}[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>])/.){<spanclass="hljs-number">3</span>,<spanclass="hljs-number">3</span>}(<spanclass="hljs-number">25</span>[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">5</span>]|(<spanclass="hljs-number">2</span>[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">4</span>]|<spanclass="hljs-number">1</span>{<spanclass="hljs-number">0</span>,<spanclass="hljs-number">1</span>}[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>]){<spanclass="hljs-number">0</span>,<spanclass="hljs-number">1</span>}[<spanclass="hljs-number">0</span>-<spanclass="hljs-number">9</span>])) 

12. 检查URL的前缀

应用开发中很多时候需要区分请求是HTTPS还是HTTP,通过下面的表达式可以取出一个url的前缀然后再逻辑判断。

<spanclass="hljs-keyword">if</span> (!s.<spanclass="hljs-keyword">match</span>(/^[a-zA-<spanclass="hljs-type">Z</span>]+:///<spanclass="hljs-comment">//))</span> {     s = <spanclass="hljs-symbol">'http</span>:<span class="hljs-comment">//' + s;</span> } 

13. 提取URL链接

下面的这个表达式可以筛选出一段文本中的URL。

^(f|ht)<spanclass="hljs-special">{</span>1<spanclass="hljs-special">}</span>(tp|tps):<spanclass="hljs-command">//</span><spanclass="hljs-command">//</span>(<spanclass="hljs-special">[</span><spanclass="hljs-command">/w</span>-<spanclass="hljs-special">]</span>+<spanclass="hljs-command">/.</span>)+<spanclass="hljs-special">[</span><spanclass="hljs-command">/w</span>-<spanclass="hljs-special">]</span>+(<spanclass="hljs-command">//</span><spanclass="hljs-special">[</span><spanclass="hljs-command">/w</span>- ./?<spanclass="hljs-comment">%&=]*)?</span> 

14. 文件路径及扩展名校验

验证windows下文件路径和扩展名(下面的例子中为.txt文件)

^(<spanclass="hljs-special">[</span>a-zA-Z<spanclass="hljs-special">]</span><spanclass="hljs-command">/:</span>|<spanclass="hljs-command">//</span>)<spanclass="hljs-command">//</span>(<spanclass="hljs-special">[</span>^<spanclass="hljs-command">//</span><spanclass="hljs-special">]</span>+<spanclass="hljs-command">//</span>)*<spanclass="hljs-special">[</span>^<spanclass="hljs-command">//</span>:*?"<>|<span class="hljs-special">]</span>+<span class="hljs-command">/.</span>txt(l)?<span class="hljs-formula">$</span> 

15. 提取Color Hex Codes

有时需要抽取网页中的颜色代码,可以使用下面的表达式。

/<spanclass="hljs-preprocessor">#([a-fA-F]|[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>]){<span class="hljs-number">1</span>,<span class="hljs-number">6</span>}</span> 

16. 提取网页图片

假若你想提取网页中所有图片信息,可以利用下面的表达式。

<spanclass="hljs-command">/<</span> *<spanclass="hljs-special">[</span>img<spanclass="hljs-special">]</span><spanclass="hljs-special">[</span>^<spanclass="hljs-command">/></span><spanclass="hljs-special">]</span>*<spanclass="hljs-special">[</span>src<spanclass="hljs-special">]</span> *= *<spanclass="hljs-special">[</span><spanclass="hljs-command">/"</span><spanclass="hljs-command">/'</span><spanclass="hljs-special">]</span><spanclass="hljs-special">{</span>0,1<spanclass="hljs-special">}</span>(<spanclass="hljs-special">[</span>^<spanclass="hljs-command">/"</span><spanclass="hljs-command">/'</span><spanclass="hljs-command">/ </span>><spanclass="hljs-special">]</span>*) 

17. 提取页面超链接

提取html中的超链接。

(<a<spanclass="hljs-command">/s*</span>(?!.*<spanclass="hljs-command">/brel</span>=)<spanclass="hljs-special">[</span>^><spanclass="hljs-special">]</span>*)(href="https?:<span class="hljs-command">//</span><span class="hljs-command">//</span>)((?!(?:(?:www<span class="hljs-command">/.</span>)?'.implode('|(?:www<span class="hljs-command">/.</span>)?', <span class="hljs-formula">$follow_list).'))<span class="hljs-special">[</span>^"<spanclass="hljs-special">]</span>+)"((?!.*<span class="hljs-command">/brel</span>=)<span class="hljs-special">[</span>^><span class="hljs-special">]</span>*)(?:<span class="hljs-special">[</span>^><span class="hljs-special">]</span>*)></span> 

18. 精炼CSS

通过下面的表达式,可以搜索相同属性值的CSS,从而达到精炼代码的目的。

^<spanclass="hljs-command">/s*</span><spanclass="hljs-special">[</span>a-zA-Z<spanclass="hljs-command">/-</span><spanclass="hljs-special">]</span>+<spanclass="hljs-command">/s*</span><spanclass="hljs-special">[</span>:<spanclass="hljs-special">]</span><spanclass="hljs-special">{</span>1<spanclass="hljs-special">}</span><spanclass="hljs-command">/s</span><spanclass="hljs-special">[</span>a-zA-Z0-9<spanclass="hljs-command">/s</span>.<spanclass="hljs-special">#</span><span class="hljs-special">]</span>+<span class="hljs-special">[</span>;<span class="hljs-special">]</span><span class="hljs-special">{</span>1<span class="hljs-special">}</span> 

19. 抽取注释

如果你需要移除HMTL中的注释,可以使用如下的表达式。

<spanclass="hljs-comment"><!--(.*?)--></span> 

20. 匹配HTML标签

通过下面的表达式可以匹配出HTML中的标签属性。

<<spanclass="hljs-command">//</span>?<spanclass="hljs-command">/w</span>+((<spanclass="hljs-command">/s</span>+<spanclass="hljs-command">/w</span>+(<spanclass="hljs-command">/s*</span>=<spanclass="hljs-command">/s*</span>(?:".*?"|'.*?'|<spanclass="hljs-special">[</span><spanclass="hljs-command">/^</span>'"><span class="hljs-command">/s</span><span class="hljs-special">]</span>+))?)+<span class="hljs-command">/s*</span>|<span class="hljs-command">/s*</span>)<span class="hljs-command">//</span>?> 

正则表达式的相关语法

下面是我找到的一张非常不错的正则表达式 Cheat Sheet,可以用来快速查找相关语法。

知道这20个正则表达式,能让你少写1,000行代码

学习正则表达式

我在网上看到了一篇相当不错的正则表达式 快速学习指南 ,有兴趣继续深入学习的同学可以参考。

知道这20个正则表达式,能让你少写1,000行代码

正则表达式在线测试工具

regex101 是一个非常不错的正则表达式在线测试工具,你可以直接在线测试你的正则表达式哦。

知道这20个正则表达式,能让你少写1,000行代码

如果你也收藏了非常有用的正则表达式,不妨也在点评中分享哦^_^

技匠,以上内容欢迎大家分享到朋友圈/微博等。如需转载,请通过简信联系授权。谢谢大家

原文  http://dataunion.org/23754.html
正文到此结束
Loading...