最全的JS判断是否为中文的方法



第一种代码:
EXFCODE:
1     function isChinese(temp)
2     {
3       var re=/[^/u4e00-/u9fa5]/;
4       if (re.test(temp)) return false ;
5       return true ;
6     }

 

第二种代码:
EXFCODE:
01     function isChn(str)
02     {
03       var reg=/^[/u4E00-/u9FA5]+$/;
04       if (!reg.test(str)){
05       alert( "不全是中文" );
06       return false ;
07     } else {
08       alert( "全是中文" );
09       return true ;
10     }

 

第三种代码:
EXFCODE:
01     function funcChina()
02     {
03       var obj = document.form1.txtName.value;
04       if (/.*[/u4e00-/u9fa5]+.*$/.test(obj))
05       {
06        alert( "不能含有汉字!" );
07        return false ;
08       } else {
09        return true ;
10       }
11     }

 

第四种代码:
EXFCODE:
1     function isChina(s)
2     {
3       var patrn=/[/u4E00-/u9FA5]|[/uFE30-/uFFA0]/gi;
4          if (!patrn.exec(s)){
5        return false ;
6          } else {
7        return true ;
8          }
9     }

第五种代码:
EXFCODE:
1     var str= '玄峰软件www.exfsoft.com' ;
2     if (escape(str).indexOf( "%u" )<0)
3     {
4       alert( "没有包含中文" );
5     } else {
6       alert( "包含中文" );
7     }

原理:escape对字符串进行编码时,字符值大于255的以"%u****"格式存储,而字符值大于255的恰好是非英文字符(一般是中文字符,非中文字符也可以当作中文字符考虑);indexOf用以判断在字符串中是否存在某子字符串,找不到返回"-1"。

原文地址:https://www.cnblogs.com/gaogaoxingxing/p/6547301.html