当前位置:网站首页>Chinese verification of JS regular expressions (turn)

Chinese verification of JS regular expressions (turn)

2022-07-04 23:22:00 Your beauty fascinates me

Today, do the input box condition verification of form submission , Verify whether Chinese is included ; Online search a circle based on js Regular expression verification is not easy to use , And most of them are from one or two original posts ! What on earth is taking doctrine . According to the search results , This article extracts the essence. , Tell you an easy-to-use Chinese verification method .

Use js Regular expressions match Chinese , Need to know Chinese characters in unicode The interval in the code . Only in this way can we understand the matching principle of expressions .

First use in regular expressions Unicode, You have to use u start , Then there are four bits of character encoding 16 Base representation
Simple Chinese matching method : /[^u0000-u00FF]/ ( Match non single byte characters )
Another wrong method :/[^u00-uFF]/ ( matching Non single byte characters 、 It also includes some width symbols, such as ,.(){}'"! etc. 、 also vwxyz character )
explain : //u0000-u00ff. contain unicode Single byte encoding ( 0-255 code ) Contains basic control characters and Latin letters . Use the negative expression , Roughly judge whether it contains Chinese .

Specific methods of matching Chinese and characters :/[u4E00-u9FA5uF900-uFA2D]/
explain : u4e00-u9fbf : unicode CJK( China, Japan and South Korea ) Unified ideograph .u9fa5 Later u9fbf It's empty
uF900-uFAFF : by unicode CJK Compatible with hieroglyphs .uFA2D Later uFAFF It's empty
For details, please refer to unicode Encoding table :http://www.nengcha.com/code/unicode/class/

javascript The code is as follows :

// Whether it contains Chinese ( It also contains Japanese and Korean ) 
    function isChineseChar(str){
         
       var reg = /[u4E00-u9FA5uF900-uFA2D]/;  
       return reg.test(str);  
    }  
    // Empathy , Whether there is a function with full angle symbol  
    function isFullwidthChar(str){
      
       var reg = /[uFF00-uFFEF]/;  
       return reg.test(str);  
    }  
原网站

版权声明
本文为[Your beauty fascinates me]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042302436199.html