当前位置:网站首页>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);
}
边栏推荐
- Analysis of the self increasing and self decreasing of C language function parameters
- A complete tutorial for getting started with redis: getting to know redis for the first time
- The small program vant tab component solves the problem of too much text and incomplete display
- QT drawing network topology diagram (connecting database, recursive function, infinite drawing, dragging nodes)
- Tweenmax emoticon button JS special effect
- S32 Design Studio for ARM 2.2 快速入门
- 【二叉树】节点与其祖先之间的最大差值
- P2181 对角线和P1030 [NOIP2001 普及组] 求先序排列
- Summary of wechat applet display style knowledge points
- SPH中的粒子初始排列问题(两张图解决)
猜你喜欢

Compare two vis in LabVIEW

MariaDB的Galera集群应用场景--数据库多主多活

蓝天NH55系列笔记本内存读写速度奇慢解决过程记录

A complete tutorial for getting started with redis: getting to know redis for the first time

Redis introduction complete tutorial: Collection details

Redis getting started complete tutorial: publish and subscribe

A complete tutorial for getting started with redis: Pipeline

初试为锐捷交换机跨设备型号升级版本(以RG-S2952G-E为例)

ETCD数据库源码分析——处理Entry记录简要流程

Tweenmax emoticon button JS special effect
随机推荐
How to choose a securities company? Is it safe to open an account on your mobile phone
Google collab trample pit
A complete tutorial for getting started with redis: redis usage scenarios
Photoshop批量给不同的图片添加不同的编号
Tweenmax emoticon button JS special effect
Redis入门完整教程:初识Redis
初试为锐捷交换机跨设备型号升级版本(以RG-S2952G-E为例)
[try to hack] wide byte injection
Notepad++ -- editing skills
Galera cluster of MariaDB - dual active and dual active installation settings
刷题指南-public
Object detection based on OpenCV haarcascades
The Chinese output of servlet server and client is garbled
A mining of edu certificate station
PS style JS webpage graffiti board plug-in
Why does infographic help your SEO
A complete tutorial for getting started with redis: getting to know redis for the first time
MariaDB的Galera集群应用场景--数据库多主多活
Redis入门完整教程:列表讲解
【二叉树】节点与其祖先之间的最大差值