当前位置:网站首页>js正则表达式之中文验证(转)
js正则表达式之中文验证(转)
2022-07-04 23:03:00 【你的美,让我痴迷】
今天做表单提交的输入框条件验证,验证是否包含中文;网上搜了一圈基于js正则表达式的验证基本不好用,而且大多都是出自一两篇原文的转帖!到底什么才是拿来主义呢。根据搜索结果,本文取精华,告诉大家一个好用的中文验证方法。
使用js正则表达式匹配中文,需要了解中文字符在unicode编码中所处的区间。这样才能够了解表达式的匹配原理。
首先在正则表达式中使用 Unicode,必须使用u开头,接着是字符编码的四位16进制表现形式
简单匹配中文方法: /[^u0000-u00FF]/ (匹配非单字节字符 )
另错误方法:/[^u00-uFF]/ (匹配 非单字节字符、还包括一些全半角符号如,.(){}'"!等、还有vwxyz字符)
说明: //u0000-u00ff.包含unicode单字节编码( 0-255编码)包含基本控制字符和拉丁文字母。 采用该否定表达式,粗略判断是否含有中文。
具体的匹配中文及字符方法:/[u4E00-u9FA5uF900-uFA2D]/
说明: u4e00-u9fbf : unicode CJK(中日韩)统一表意字符。u9fa5后至u9fbf为空
uF900-uFAFF : 为unicode CJK 兼容象形文字 。uFA2D后至uFAFF为空
具体可参考unicode编码表:http://www.nengcha.com/code/unicode/class/
javascript代码如下所示:
//是否含有中文(也包含日文和韩文)
function isChineseChar(str){
var reg = /[u4E00-u9FA5uF900-uFA2D]/;
return reg.test(str);
}
//同理,是否含有全角符号的函数
function isFullwidthChar(str){
var reg = /[uFF00-uFFEF]/;
return reg.test(str);
}
边栏推荐
- MariaDB的Galera集群-双主双活安装设置
- Redis入门完整教程:列表讲解
- Summary of wechat applet display style knowledge points
- Photoshop批量给不同的图片添加不同的编号
- Analysis of the self increasing and self decreasing of C language function parameters
- Examples of time (calculation) total tools: start time and end time of this year, etc
- Advantages of Alibaba cloud international CDN
- Redis:Redis消息的发布与订阅(了解)
- 云服务器设置ssh密钥登录
- Redis: redis configuration file related configuration and redis persistence
猜你喜欢

MariaDB的Galera集群-双主双活安装设置
![[Jianzhi offer] 6-10 questions](/img/73/5974068008bcdc9a70b3f5f57f1eb0.png)
[Jianzhi offer] 6-10 questions

A mining of edu certificate station

The small program vant tab component solves the problem of too much text and incomplete display

phpcms付费阅读功能支付宝支付

C语言快速解决反转链表

Analysis of the self increasing and self decreasing of C language function parameters

QT addition calculator (simple case)

SPH中的粒子初始排列问题(两张图解决)

CTF競賽題解之stm32逆向入門
随机推荐
Ffmpeg quick clip
Why does infographic help your SEO
How to choose a securities company? Is it safe to open an account on your mobile phone
【图论】拓扑排序
Intelligence test to see idioms guess ancient poems wechat applet source code
A complete tutorial for getting started with redis: Pipeline
Compare two vis in LabVIEW
colResizable. JS auto adjust table width plug-in
Explanation of bitwise operators
Qt个人学习总结
P2181 diagonal and p1030 [noip2001 popularization group] arrange in order
[graph theory] topological sorting
Photoshop batch adds different numbers to different pictures
Redis getting started complete tutorial: Key Management
Redis: redis transactions
cout/cerr/clog的区别
[ODX studio edit PDX] - 0.2-how to compare two pdx/odx files of compare
初试为锐捷交换机跨设备型号升级版本(以RG-S2952G-E为例)
时间 (计算)总工具类 例子: 今年开始时间和今年结束时间等
为什么信息图会帮助你的SEO