Php/js Chinese character regular expression summary

Source: Internet
Author: User

 

Js version

Regular Expression Matching Chinese characters: [/u4e00-/u9fa5]

Match double-byte characters (including Chinese characters): [^/x00-/xff]

The code is as follows: Copy code


Var reg =/^ [u4e00-u9fa5] + $ /;

If (reg. test (str ))
{
Alert ('work of Chinese characters ');
}

Calculate the length of a string (two-byte length meter 2, ASCII character meter 1)

String. prototype. len = function () {return this. replace ([^/x00-/xff]/g, "aa"). length ;}

Php version

Php regular expression matches Chinese characters!
/^ [X {4e00}-x {9fa5}] + $/u

The code is as follows: Copy code

 

$ Action = trim ($ _ GET ['action']);
If ($ action = "sub ")
{
$ Str = $ _ POST ['dir'];
// If (! Preg_match ("/^ [". chr (0xa1 ). "-". chr (0xff ). "A-Za-z0-9 _] + $/", $ str) // GB2312 regular expression of Chinese characters, letters, numbers, underscores (_)
If (! Preg_match ("/^ [x {4e00}-x {9fa5} A-Za-z0-9 _] + $/u", $ str) // UTF-8 Chinese characters, letters, numbers, underscores, regular expressions
    { 
Echo "the [". $ str. "] you entered contains illegal characters ";
    }
Else
    {
Echo "the [". $ str. "] you entered is completely legal. Pass! ";
    }
}

Of course, if you want to match the GBK2312 encoding of all Chinese characters:

The code is as follows: Copy code


$ Str = "example ";
If (preg_match ("/^ [". chr (0xa1). "-". chr (0xff). "] + $/", $ str )){
Print ($ str. "all Chinese characters are true ");
} Else {
Print ($ str. "This really TMD is not all Chinese characters ");
}

Uft8 encoding regular

$ Str = "Chinese characters ";
If (preg_match ("/^ [x {4e00}-x {9fa5}] + $/u", $ str )){
Print ("all strings are Chinese ");
} Else {
Print ("Not all strings are Chinese ");
}


 

In fact, as long as you understand the beginning and end of each encoding's high and low levels, you can naturally write regular expressions and directly use sixteen digits. What are the difficulties? Haha. Note that in php, the sixteen bits use x.

Example of gbk and gb2312:

The code is as follows: Copy code

<? Php
$ Action = trim ($ _ GET ['action']);
If ($ action = "sub ")
{
$ Str = $ _ POST ['dir'];
// If (! Preg_match ("/^ [". chr (0xa1 ). "-". chr (0xff ). "A-Za-z0-9 _] + $/", $ str) // GB2312 regular expression of Chinese characters, letters, numbers, underscores (_)
If (! Preg_match ("/^ [x {4e00}-x {9fa5} A-Za-z0-9 _] + $/u", $ str) // UTF-8 Chinese characters, letters, numbers, underscores, regular expressions
    {  
Echo "<font color = red> the [". $ str. "] you entered contains illegal characters </font> ";
     }
Else
     {
Echo "<font color = green> the [". $ str. "] you entered is completely legal. Pass! </Font> ";
     }
}
?>

 

+ $/U means:

+ Indicates repeated once or multiple times;
$ Indicates the end of the match;
/Indicates the delimiters;
U indicates that the pattern string is treated as a UTF-8;
U indicates that the search is stopped after the first match.

Match 2-4, represented by {2, 4.
/^ [X {4e00}-x {9fa5}] {2, 4} $/u

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.