DWQA QuestionsCategory: ProgramThe problem of JS regular expression matching Chinese
Code Monkey asked 1 week ago
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <script>
        Var result = (New regexp ("/ ^ [\ u4e00 - \ u9fa5] $/)). Test (" Institute ");
        console.log(result);
    </script>
</head>
<body>
</body>
</html>

Why is the output false? Use VIM: set fileencoding to confirm that the file is encoded in UTF-8

Humphry replied 1 week ago

New regexp ("^ [\ u4e00 - \ u9fa5] $"). Test ("Institute");perhaps/^[\ u4e00 - \ u9fa5] $/. Test ("Institute");

7 Answers
Best Answer
Bubble answered 1 week ago

Var result = (New regexp (“^ [\ \ u4e00 – \ \ u9fa5] $”)). Test (“Institute”);
console.log(result);

n͛i͛g͛h͛t͛i͛r͛e͛ answered 1 week ago

It’s nothing to do with vim. It turned out to befalse, print it in consolenew RegExp("/^[\u4e00-\u9fa5]$/")I know.

gaara replied 1 week ago

Is there a backslash around the regular expression?

Flying dragon answered 1 week ago
Var result = (New regexp ("^ [\ \ u4e00 - \ \ u9fa5] $")). Test ("Institute");

perhaps

Var result = / ^ [\ u4e00 - \ u9fa5] $/. Test ("Institute");
fishenal answered 1 week ago

JS / / is the literal amount of regular expression. After new regexp is created, the parameter is regular string. The main topic is confusing the concept

qingwa answered 1 week ago

/[\u4E00-\u9FA5\uF900-\uFA2D]/.test(value);
In fact, many of them are not easy to use!! Try this.