Min's blog

I choose to see the beauties in the world.

导航

判断中英文

Posted on 2017-07-27 10:00  Min77  阅读(170)  评论(0编辑  收藏  举报
 var str= '玄峰软件www.exfsoft.com' ; 
 if (escape(str).indexOf( "%u" )<0){ 
     alert( "没有包含中文" ); 
}else{
     alert( "包含中文" ); 
}

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