JS to determine whether the string contains numbers, English letters, Chinese characters

Source: Internet
Author: User

 1, use JS to determine whether a string contains numbers, English letters, Chinese characters 
String str = "This is string ";
1>
var a =/[0-9]/;
var b = a.test (str ),//true, indicating a number
2>
var a =/[a-z]/i;
var b = a.test (str ),//true, indicating the English alphabet
3>
var a = new RegExp (" [\\u4e00-\\u9fff]+ "," G ");
var B = a.test (str) ;//true, description
4>
var a =/[0-9a-z]/i;
var b = a.test (str)//true, description have English letters or numbers
 2, judging string length 
String str = "This is
1> does not contain Chinese characters
var len = str . length;
2> the number of characters in a string of pure Chinese characters
var a =/[\u4e00-\u9fa5]/g;
var len = Str.match (a). length;
3> in English mixed string length, English characters plus 1, Kanji plus 2
function strlen (str) {
    var len = 0;
    for (var i=0; i<str.length; i++) {
     var c = str.charcodeat (i);
   //single byte plus 1
     if ((c >= 0x0001 && C <= 0x007e) | | (0xff60<=c && c<=0xff9f)) {
       len++;
    }
     Else {
//Kanji plus 2
      len+=2;
    
   }
    return len;
}

var len = strlen (str);

JS to determine whether the string contains numbers, English letters, kanji

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.