Home >Web Front-end >JS Tutorial >jquery method to determine whether it is a Chinese character

jquery method to determine whether it is a Chinese character

藏色散人
藏色散人Original
2020-12-03 09:10:254199browse

Jquery method to determine whether it is a Chinese character: first create a front-end sample file; then enter the regular expression used to match Chinese as "\u4E00-\u9FA5"; then use test or match to detect whether the string matches Regular matching; finally output the judgment result.

jquery method to determine whether it is a Chinese character

The operating environment of this tutorial: Windows 7 system, jquery version 1.10.0. This method is suitable for all brands of computers.

Recommended: "jquery video tutorial"

How to use regular expressions in jquery to determine whether it contains Chinese and whether it is all Chinese

Introduction In jquery, use regular expressions to verify whether it contains Chinese or whether it is all Chinese specific method codes, and there is a complete example demonstration.

In jquery, if we want to determine whether a variable or the content entered by the user in the text box contains Chinese, or whether it is all Chinese, the method is very simple,

The following , the webmaster explains step by step to show you how to write the expression we need in practice:

First, the regular expression used to match Chinese is:

\u4E00-\u9FA5

Secondly, in regular expressions, when we want to verify or match a string, we can use test or match:

test: used to detect whether a string matches a regular expression and returns true or false ;

Syntax structure: regular.test(string)

match: Used to retrieve values ​​within the string that match the regular expression. If no matching value is found, null is returned.

Syntax structure: string.match(regular);

After understanding the above two steps, we can start writing the regular expression we need:

var v="要检验的字符串";
var d=/[\u4E00-\u9FA5]+/.test(v);//检测v里是否包含1个或多个中文字符
或者:
var d=v.match(/[\u4E00-\u9FA5]+/);//匹配v里1个或多个连续的中文字符

In the above code, as long as v contains Chinese, then d is true. This meets our requirement of "judging whether it contains Chinese". Then if we want to determine whether the string is all Chinese, we only need to Just add symbols indicating the beginning and end of the string to the regular expression:

var v="要检验的字符串";
var d=/^[\u4E00-\u9FA5]+$/.test(v);//检测v从开头到结尾是否为一个或多个中文
或者:
var d=v.match(/^[\u4E00-\u9FA5]+$/);//匹配v从开头到结尾1个或多个连续的中文字符

The above is the detailed content of jquery method to determine whether it is a Chinese character. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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