In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
这篇文章给大家分享的是有关WEB开发时常用的正则表达式有哪些的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
在WEB开发中,正则表达式通常用来检测、查找替换某些符合规则的字符串,如检测用户输入E-mai格式是否正确,采集符合规则的页面内容等等。
下面分别用PHP和Javscript向大家介绍WEB开发中最常用最实用的正则表达式及其用法。
PHP常用表达式用法
1.匹配正整数:/^[1-9]\d*$/
2.匹配非负整数(正整数+0):/^\d+$/
3.匹配中文:/^[\x{4e00}-\x{9fa5}]+$/u
4.匹配Email:/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
5.匹配网址URL:(((f|ht){1}(tp|tps)://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)
6.匹配字母开头,5-16字符,字母数字下划线:/^[a-zA-Z][a-zA-Z0-9_]{4,15}$/
7.匹配数字,字母,下划线,中文:/^[\x{4e00}-\x{9fa5}A-Za-z0-9_]+$/u
8.匹配中国邮政编码:/^[1-9]\d{5}$/
9.匹配IP地址:/\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/
10.匹配中国大陆身份证:/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}(\d|x|X)$/
PHP正则验证字符串方法举例:
$str = "中文啊"; $preg = "/^[\x{4e00}-\x{9fa5}]+$/u"; //匹配中文 if(preg_match($preg,$str,$arr)){ $msg = '匹配成功!'; }else{ $msg = '匹配失败!'; } echo $msg;
Javascript常用表达式用法
1.匹配正整数:/^[0-9]*[1-9][0-9]*$/
2.匹配非负整数(正整数+0):/^\d+$/
3.匹配中文:/^[\u4e00-\u9fa5]/
4.匹配Email:/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
5.匹配网址URL:/^(f|ht){1}(tp|tps):\/\/([\w-]+\.)+[\w-]+(\/[\w- ./?%&=]*)?/
6.匹配字母开头,5-16字符,字母数字下划线:/^[a-zA-Z][a-zA-Z0-9_]{4,15}$/
7.匹配数字,字母,下划线,中文:/^[\u4e00-\u9fa5A-Za-z0-9_]+$/
8.匹配中国邮政编码:/^[1-9]\d{5}$/
9.匹配IP地址:/\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/
10.匹配中国大陆身份证:/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}(\d|x|X)$/
Javascript正则验证字符串方法举例:
var str = "abc@126.com"; var preg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; //匹配Email if(preg.test(str)){ var msg = "匹配成功"; }else{ var msg = "匹配失败!"; } alert(msg);感谢各位的阅读!关于"WEB开发时常用的正则表达式有哪些"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.