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

Standard

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

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

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

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

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


1 . 校验密码强度

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

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

2. 校验中文

字符串仅能是中文。

<code class="tex">^<span class="hljs-special">[</span><span class="hljs-command">\\</span>u4e00-<span class="hljs-command">\\</span>u9fa5<span class="hljs-special">]</span><span class="hljs-special">{</span>0,<span class="hljs-special">}</span><span class="hljs-formula">$</span></code>

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

<code class="tex">^<span class="hljs-command">\\</span>w+<span class="hljs-formula">$</span></code>

4. 校验E-Mail 地址

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

<code class="markdown">[<span class="hljs-link_label">\\w!#$%&amp;'*+/=?^_`{|}~-</span>]+(?:\\.[<span class="hljs-link_label">\\w!#$%&amp;'*+/=?^_`{|}~-</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>)?</code>

5. 校验身份证号码

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

15位:

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

18位:

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

6. 校验日期

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

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

7. 校验金额

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

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

8. 校验手机号

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

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

9. 判断IE的版本

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

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

10. 校验IP-v4地址

IP4 正则语句。

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

11. 校验IP-v6地址

IP6 正则语句。

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

12. 检查URL的前缀

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

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

13. 提取URL链接

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

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

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

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

<code class="tex">^(<span class="hljs-special">[</span>a-zA-Z<span class="hljs-special">]</span><span class="hljs-command">\\</span>:|<span class="hljs-command">\\</span><span class="hljs-command">\\</span>)<span class="hljs-command">\\</span><span class="hljs-command">\\</span>(<span class="hljs-special">[</span>^<span class="hljs-command">\\</span><span class="hljs-command">\\</span><span class="hljs-special">]</span>+<span class="hljs-command">\\</span><span class="hljs-command">\\</span>)*<span class="hljs-special">[</span>^<span class="hljs-command">\\</span>/:*?"&lt;&gt;|<span class="hljs-special">]</span>+<span class="hljs-command">\\</span>.txt(l)?<span class="hljs-formula">$</span></code>

15. 提取Color Hex Codes

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

<code class="cpp">^<span class="hljs-preprocessor">#([A-Fa-f0-<span class="hljs-number">9</span>]{<span class="hljs-number">6</span>}|[A-Fa-f0-<span class="hljs-number">9</span>]{<span class="hljs-number">3</span>})$</span></code>

16. 提取网页图片

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

<code class="tex"><span class="hljs-command">\\</span>&lt; *<span class="hljs-special">[</span>img<span class="hljs-special">]</span><span class="hljs-special">[</span>^<span class="hljs-command">\\</span><span class="hljs-command">\\</span>&gt;<span class="hljs-special">]</span>*<span class="hljs-special">[</span>src<span class="hljs-special">]</span> *= *<span class="hljs-special">[</span><span class="hljs-command">\\</span>"<span class="hljs-command">\\</span>'<span class="hljs-special">]</span><span class="hljs-special">{</span>0,1<span class="hljs-special">}</span>(<span class="hljs-special">[</span>^<span class="hljs-command">\\</span>"<span class="hljs-command">\\</span>'<span class="hljs-command">\\</span> &gt;<span class="hljs-special">]</span>*)</code>

17. 提取页面超链接

提取html中的超链接。

<code class="tex">(&lt;a<span class="hljs-command">\\</span>s*(?!.*<span class="hljs-command">\\</span>brel=)<span class="hljs-special">[</span>^&gt;<span class="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>^"<span class="hljs-special">]</span>+)"((?!.*<span class="hljs-command">\\</span>brel=)<span class="hljs-special">[</span>^&gt;<span class="hljs-special">]</span>*)(?:<span class="hljs-special">[</span>^&gt;<span class="hljs-special">]</span>*)&gt;</span></code>

18. 查找CSS属性

通过下面的表达式,可以搜索到相匹配的CSS属性。

<code class="tex">^<span class="hljs-command">\\</span>s*<span class="hljs-special">[</span>a-zA-Z<span class="hljs-command">\\</span>-<span class="hljs-special">]</span>+<span class="hljs-command">\\</span>s*<span class="hljs-special">[</span>:<span class="hljs-special">]</span><span class="hljs-special">{</span>1<span class="hljs-special">}</span><span class="hljs-command">\\</span>s<span class="hljs-special">[</span>a-zA-Z0-9<span class="hljs-command">\\</span>s.<span class="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></code>

19. 抽取注释

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

<code class="xml"><span class="hljs-comment">&lt;!--(.*?)--&gt;</span></code>

20. 匹配HTML标签

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

<code class="tex">&lt;<span class="hljs-command">\\</span>/?<span class="hljs-command">\\</span>w+((<span class="hljs-command">\\</span>s+<span class="hljs-command">\\</span>w+(<span class="hljs-command">\\</span>s*=<span class="hljs-command">\\</span>s*(?:".*?"|'.*?'|<span class="hljs-special">[</span><span class="hljs-command">\\</span>^'"&gt;<span class="hljs-command">\\</span>s<span class="hljs-special">]</span>+))?)+<span class="hljs-command">\\</span>s*|<span class="hljs-command">\\</span>s*)<span class="hljs-command">\\</span>/?&gt;</code>

正则表达式的相关语法

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


学习正则表达式

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


正则表达式在线测试工具

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


转:http://www.jianshu.com/p/e7bb97218946

Leave a Reply

Your email address will not be published. Required fields are marked *