当前位置:网站首页>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);
}
边栏推荐
- 【ODX Studio编辑PDX】-0.2-如何对比Compare两个PDX/ODX文件
- 时间 (计算)总工具类 例子: 今年开始时间和今年结束时间等
- Redis introduction complete tutorial: List explanation
- Phpcms paid reading function Alipay payment
- MariaDB的Galera集群-双主双活安装设置
- D3.js+Three. JS data visualization 3D Earth JS special effect
- SPH中的粒子初始排列问题(两张图解决)
- Redis入门完整教程:集合详解
- A complete tutorial for getting started with redis: getting to know redis for the first time
- Stm32 Reverse Introduction to CTF Competition Interpretation
猜你喜欢
Set up a website with a sense of ceremony, and post it to 1/2 of the public network through the intranet
Complete tutorial for getting started with redis: bitmaps
C language to quickly solve the reverse linked list
位运算符讲解
SPH中的粒子初始排列问题(两张图解决)
ICML 2022 || 3DLinker: 用于分子链接设计的E(3)等变变分自编码器
Redis démarrer le tutoriel complet: Pipeline
Redis getting started complete tutorial: publish and subscribe
[Jianzhi offer] 6-10 questions
一次edu证书站的挖掘
随机推荐
Object detection based on OpenCV haarcascades
Question brushing guide public
Redis入門完整教程:Pipeline
MariaDB's Galera cluster application scenario -- multi master and multi active databases
The solution to the lack of pcntl extension under MAMP, fatal error: call to undefined function pcntl_ signal()
ECS settings SSH key login
Excel 快捷键-随时补充
Recommended collection: build a cross cloud data warehouse environment, which is particularly dry!
【爬虫】数据提取之JSONpath
The initial arrangement of particles in SPH (solved by two pictures)
LIst 相关待整理的知识点
HMS core unified scanning service
Examples of time (calculation) total tools: start time and end time of this year, etc
Redis getting started complete tutorial: Key Management
【js】-【排序-相关】-笔记
图片懒加载的原理
JS card style countdown days
D3.js+Three. JS data visualization 3D Earth JS special effect
Mysql database backup and recovery -- mysqldump command
PS style JS webpage graffiti board plug-in