Home  >  Article  >  Backend Development  >  Regular expressions that save 1,000 lines of code

Regular expressions that save 1,000 lines of code

小云云
小云云Original
2017-12-09 11:43:301432browse

正则表达式,一个十分古老而又强大的文本处理工具,仅仅用一段非常简短的表达式语句,便能够快速实现一个非常复杂的业务逻辑。知道这20个正则表达式,能让你少写1000行代码,本文就和大家分享可以少写1000行代码的正则表达式方法,希望能帮助到大家。

熟练地掌握正则表达式的话,能够使你的开发效率得到极大的提升。

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

var reg = /^(\\d{1,4})(-|\\/)(\\d{1,2})\\2(\\d{1,2})$/;
var r = fieldValue.match(reg);  
if(r==null)alert('Date format error!');

下面是在前端开发中经常使用到的20个正则表达式:

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


复制代码 代码如下:

^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$ 

2. 校验中文
字符串仅能是中文。


复制代码 代码如下:

^[\\u4e00-\\u9fa5]{0,}$


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

^\\w+$ 

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


复制代码 代码如下:

[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])? 

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

15位:

复制代码 代码如下:

^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$

18位:

复制代码 代码如下:

^[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)$ 

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


复制代码 代码如下:

^(?:(?!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)$

 

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

^[0-9]+(.[0-9]{2})?$ 

8. 校验手机号
下面是国内 13、15、18开头的手机号正则表达式。


复制代码 代码如下:

^(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}$ 

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


复制代码 代码如下:

^.*MSIE [5-8](?:\\.[0-9]+)?(?!.*Trident\\/[5-9]\\.0).*$ 

10. 校验IP-v4地址
IP4 正则语句。


复制代码 代码如下:

\\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 

11. 校验IP-v6地址
IP6 正则语句。


复制代码 代码如下:

(([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]))

 

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

if (!s.match(/^[a-zA-Z]+:\\/\\//))
{
 s = 'http://' + s;
}

13. 提取URL链接
下面的这个表达式可以筛选出一段文本中的URL。


Copy code The code is as follows:

^(f|ht){1}(tp|tps):\\/\\/([\\w-]+\ \.)+[\\w-]+(\\/[\\w- ./?%&=]*)?

14. File path and extension verification
Verify file Path and extension


Copy code The code is as follows:

([a-zA-Z]\\:|\\ \\)\\\\([^\\\\]+\\\\)*[^\\/:*?"a8093152e673feb7aba1828c43532094|]+\\.txt(l)?$

15. Extract Color Hex Codes
Sometimes you need to extract the color codes in the web page, you can use the following expression

\\#([a-fA-F]|[0-9 ]){3,6}

16. Extract web page images
If you want to extract all the image information in the web page, you can use the following expression.


Copy code The code is as follows:

\\21a9f2105e48b750c443442baf568f72]*[src] *= *[\\"\\'] {0,1}([^\\"\\'\\ >]*)

17. Extract page hyperlinks
Extract hyperlinks in html.


Copy code The code is as follows:

(29432ce042724116da9c6317a82ffcef;]*)(?:[^>;]*)>

18. Refining CSS
Through the following expression, you can search for CSS with the same attribute value to achieve the purpose of refining the code.


Copy code The code is as follows:

^\\s*[a-zA-Z\\-]+\\ s*[:]{1}\\s[a-zA-Z0-9\\s.#]+[;]{1}

19. Extract comments
If you need to remove Comments in HMTL can use the following expressions.

323fb0f7fe6aafe3a77b6c8f8c2d39b1

20. Match HTML tags
You can match tags in HTML through the following expression.


Copy code The code is as follows:

9182401fd27e2140530df0785329a1cb\\s]+))?)+\\s*|\\s*) /?>

Related recommendations:

Regular expression implementation steps

Detailed explanation of commonly used functions in php regular expressions

Detailed syntax of PHP regular expressions

The above is the detailed content of Regular expressions that save 1,000 lines of code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn