Home > Web Front-end > JS Tutorial > body text

How to use regular expressions to match Chinese characters in PHP and JS

php中世界最好的语言
Release: 2018-03-29 17:08:25
Original
1397 people have browsed it

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 and

javascript. 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);
}
Copy after login

Regular match in php gbk, gb2312 Chinese:

preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/",$str)
Copy after login

javascript regular match Chinese:

var str = "php编程";
if (/^[\u4e00-\u9fa5]+$/.test(str)) {
alert("该字符串全部是中文");
} else {
alert("该字符串不全部是中文");
}
Copy after login

# 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template