Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How regular expressions match both Chinese and English

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

小编给大家分享一下正则表达式如何同时匹配中英文,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

匹配中文:[\u4e00-\u9fa5]

英文字母:[a-zA-Z]

数字:[0-9]

匹配中文,英文字母和数字及_:

^[\u4e00-\u9fa5_a-zA-Z0-9]+$

同时判断输入长度:

[\u4e00-\u9fa5_a-zA-Z0-9_]{4,10}

^[\w\u4E00-\u9FA5\uF900-\uFA2D]*$

1、一个正则表达式,只含有汉字、数字、字母、下划线不能以下划线开头和结尾:

^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$ 其中:

^ 与字符串开始的地方匹配

(?!_) 不能以_开头

(?!.*?_$) 不能以_结尾

[a-zA-Z0-9_\u4e00-\u9fa5]+ 至少一个汉字、数字、字母、下划线

$ 与字符串结束的地方匹配

放在程序里前面加@,否则需要\\进行转义 @"^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$"

(或者:@"^(?!_)\w*(?

只能输入汉字:"^[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)应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计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.9499.net/page1.htm";

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

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

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

"onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^u4E00-u9FA5]/g,'))"

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

"onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^uFF00-uFFFF]/g,'))"

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

('text',clipboardData.getData('text').replace(/[^d]/g,'))"

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

('text',clipboardData.getData('text').replace(/[^d]/g,'

以上是"正则表达式如何同时匹配中英文"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report