Front-end basic HTML. Common regular matching

Regular expression to match Chinese characters:  [u4e00-u9fa5] 

Comment: Matching Chinese is really a headache, with this expression it is easy to handle 


Match double-byte characters ( including Chinese characters ) : [^x00-xff] 

Comment: Can be used to calculate the length of a string (a double-byte character counts as 2 , and an ASCII character counts as 1 ) 


Regular expression to match blank lines: ns*r 

Comment: Can be used to delete blank lines 


Regular expression to match HTML tags: < (S*?)[^>]*>.*?|< .*? /> 

Comment: The version circulating on the Internet is too bad. The above one can only match the part, and it is still powerless for complex nested tags. 


Regular expression to match leading and trailing whitespace characters: ^s*|s*$ 

Comment: Can be used to remove whitespace characters ( including spaces, tabs, form feeds, etc. ) at the beginning and end of a line , very useful expression 


Regular expression to match email addresses: w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)* 

Comment: useful for form validation 


Regular expression to match URL : [a-zA-z]+://[^s]* 

Comment: The version circulating on the Internet has very limited functions, and the above can basically meet the needs 


Whether the matching account number is legal ( begins with letters, allows 5-16 bytes, and allows alphanumeric underscores ) : ^[a-zA-Z][a-zA-Z0-9_]{4,15}$ 

Comment: useful for form validation 


Match domestic phone numbers: d{3}-d{8}|d{4}-d{7} 

Comment: Match the form such as  0511-4405222  or  021-87888822 


Match Tencent QQ number: [1-9][0-9]{4,} 

Comment: Tencent QQ account starts from 10000 


Match Chinese postal code: [1-9]d{5}(?!d) 

Comment: China postal code is 6 digits 


Match ID: d{15}|d{18} 

Comments: Chinese ID cards are 15 or 18 digits 


Match ip address: d+.d+.d+.d+ 

Comment: Useful when extracting ip addresses 



Match a specific number: 

^[1-9]d*$  // Matches positive integers    

^-[1-9]d*$   // Matches negative integers  

^-?[1-9]d*$   //匹配整数 

^[1-9]d*|0$  //匹配非负整数(正整数 + 0 

^-[1-9]d*|0$   //匹配非正整数(负整数 + 0 

^[1-9]d*.d*|0.d*[1-9]d*$   //匹配正浮点数 

^-([1-9]d*.d*|0.d*[1-9]d*)$  //匹配负浮点数 

^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0+|0)$  //匹配浮点数 

^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$   //匹配非负浮点数(正浮点数 + 0 

^(-([1-9]d*.d*|0.d*[1-9]d*))|0?.0+|0$  //匹配非正浮点数(负浮点数 + 0 

评注:处理大量数据时有用,具体应用时注意修正 


匹配特定字符串: 

^[A-Za-z]+$  //匹配由26个英文字母组成的字符串 

^[A-Z]+$  //匹配由26个英文字母的大写组成的字符串 

^[a-z]+$  //匹配由26个英文字母的小写组成的字符串 

^[A-Za-z0-9]+$  //匹配由数字和26个英文字母组成的字符串 

^w+$  //匹配由数字、26个英文字母或者下划线组成的字符串 


在使用RegularExpressionValidator验证控件时的验证功能及其验证表达式介绍如下

只能输入数字:“^[0-9]*$” 

只能输入n位的数字:“^d{n}$” 

只能输入至少n位数字:“^d{n,}$” 

只能输入m-n位的数字:“^d{m,n}$” 

只能输入零和非零开头的数字:“^(0|[1-9][0-9]*)$” 

只能输入有两位小数的正实数:“^[0-9]+(.[0-9]{2})?$” 

只能输入有1-3位小数的正实数:“^[0-9]+(.[0-9]{1,3})?$” 

只能输入非零的正整数:“^+?[1-9][0-9]*$” 

只能输入非零的负整数:“^-[1-9][0-9]*$” 

只能输入长度为3的字符:“^.{3}$” 

只能输入由26个英文字母组成的字符串:“^[A-Za-z]+$” 

只能输入由26个大写英文字母组成的字符串:“^[A-Z]+$” 

只能输入由26个小写英文字母组成的字符串:“^[a-z]+$” 

只能输入由数字和26个英文字母组成的字符串:“^[A-Za-z0-9]+$” 

只能输入由数字、26个英文字母或者下划线组成的字符串:“^w+$” 

验证用户密码:“^[a-zA-Z]w{5,17}$”正确格式为:以字母开头,长度在6-18之间, 



只能包含字符、数字和下划线。 

验证是否含有^%&',;=?$"等字符:“[^%&',;=?$x22]+” 

只能输入汉字:“^[u4e00-u9fa5],{0,}$” 

验证Email地址:“^w+[-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$” 

验证InternetURL“^http://([w-]+.)+[w-]+(/[w-./?%&=]*)?$” 

验证电话号码:“^((d{3,4})|d{3,4}-)?d{7,8}$” 



正确格式为:“XXXX-XXXXXXX”“XXXX-XXXXXXXX”“XXX-XXXXXXX” 



“XXX-XXXXXXXX”“XXXXXXX”“XXXXXXXX” 

验证身份证号(15位或18位数字):“^d{15}|d{}18$” 

验证一年的12个月:“^(0?[1-9]|1[0-2])$”正确格式为:“01”-“09”“1”“12” 

验证一个月的31天:“^((0?[1-9])|((1|2)[0-9])|30|31)$” 



正确格式为:“01”“09”“1”“31” 



匹配中文字符的正则表达式: [u4e00-u9fa5] 

匹配双字节字符(包括汉字在内)[^x00-xff] 

匹配空行的正则表达式:n[s| ]*r 

匹配HTML标记的正则表达式:/< (.*)>.*|< (.*) />/ 

匹配首尾空格的正则表达式:(^s*)|(s*$) 

匹配Email地址的正则表达式:w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)* 

匹配网址URL的正则表达式:http://([w-]+.)+[w-]+(/[w- ./?%&=]*)? 


(1)应用:计算字符串的长度(一个双字节字符长度计2ASCII字符计1 

String.prototype.len=function(){return this.replace([^x00-xff]/g,"aa").length;} 


(2)应用:javascript中没有像vbscript那样的trim函数,我们就可以利用这个表达式来实现 

String.prototype.trim = function() 

return this.replace(/(^s*)|(s*$)/g, ""); 

(3)应用:利用正则表达式分解和转换IP地址 

function IP2V(ip) //IP地址转换成对应数值 

re=/(d+).(d+).(d+).(d+)/g //匹配IP地址的正则表达式 

if(re.test(ip)) 

return RegExp.$1*Math.pow(255,3))+RegExp.$2*Math.pow(255,2))+RegExp.$3*255+RegExp.$4*1 

else 

throw new Error("Not a valid IP address!") 

4)应用:从URL地址中提取文件名的javascript程序 

s="http://www.jb51.net/page1.htm"; 

s=s.replace(/(.*/){0,}([^.]+).*/ig,"$2") ; //Page1.htm 

(5)应用:利用正则表达式限制网页表单里的文本框输入内容 

用正则表达式限制只能输入中文:onkeyup="value="/blog/value.replace(/["^u4E00-u9FA5]/g,'') " onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^u4E00-u9FA5]/g,''))" 

用正则表达式限制只能输入全角字符: onkeyup="value="/blog/value.replace(/["^uFF00-uFFFF]/g,'') " onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^uFF00-uFFFF]/g,''))" 

用正则表达式限制只能输入数字:onkeyup="value="/blog/value.replace(/["^d]/g,'') "onbeforepaste= "clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]/g,''))" 

用正则表达式限制只能输入数字和英文:onkeyup="value="/blog/value.replace(/[W]/g,"'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]/g,''

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325621741&siteId=291194637