Home > Article > Web Front-end > How to use regular expressions to match Chinese characters in PHP and JS
This time I will show you how to use regular expressions to match Chinese characters in php and JS. What are the precautions for using regular expressions to match Chinese characters in php and JS. The following is a practical case. Let’s take a look. .
The example in this article describes the method of matching Chinese with php andjavascript. Share it with everyone for your reference, the details are as follows:
regular matching utf-8 Chinese in php: (the emphasis is: [\x{4e00}-\x{9fa5} ]+)
$str = "脚本之家"; if (preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",$str,$arr)) { print("该字符串全部是中文"); echo '<pre class="brush:php;toolbar:false">'; print_r($arr); } else { print("该字符串不全部是中文"); echo '<pre class="brush:php;toolbar:false">'; print_r($arr); }
Regular match in php gbk, gb2312 Chinese:
preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/",$str)
javascript regular match Chinese:
var str = "php编程"; if (/^[\u4e00-\u9fa5]+$/.test(str)) { alert("该字符串全部是中文"); } else { alert("该字符串不全部是中文"); }
# I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:
Detailed explanation of the use of lastIndex in regular expressions
Detailed explanation of the use of zero-width assertion based on PHP regular expressions
The above is the detailed content of How to use regular expressions to match Chinese characters in PHP and JS. For more information, please follow other related articles on the PHP Chinese website!