JavaScript 字符串 normalize() 方法
JavaScript String normalize() 方法用于检索给定输入字符串的 Unicode 范式。如果输入不是字符串,则该方法会在执行前将其转换为字符串。默认的范式为"NFC",即范式规范组合。
它接受一个名为"form"的可选参数,用于指定 Unicode 范式,其值如下:
- NFC − 范式规范组合。
- NFD − 范式规范分解。
- NFKC − 范式兼容性组合。
- NFKD − 范式兼容性分解。
语法
以下是 JavaScript 字符串 normalize() 方法的语法:
normalize(form)
参数
此方法接受一个名为"form"的参数,如下所述 -
- form(可选) - 它指定 Unicode 范式。
返回值
此方法返回一个包含输入字符串 Unicode 范式的新字符串。
示例 1
如果省略 form 参数,该方法将使用默认范式"NFC"。
在下面的示例中,我们使用 JavaScript 字符串 normalize() 方法来检索给定字符串"Tutorials Point"的 Unicode 范式。由于我们省略了 form 参数,因此该方法将使用默认范式,即 NFC。
<html>
<head>
<title>JavaScript String normalize() Method</title>
</head>
<body>
<script>
const str = "Tutorials Point";
document.write("The given string: ", str);
//使用 normalize() 方法
var new_str = str.normalize();
document.write("<br>The Unicode normalization of the given string: ", new_str);
</script>
</body>
</html>
输出
上述程序返回一个包含 Unicode 范式的字符串,格式为:
The given string: Tutorials Point The Unicode normalization of the given string: Tutorials Point
示例 2
当您将特定的范式"NFKC"作为参数传递给此方法时,它会根据该范式修改字符串。
以下是 JavaScript 字符串 normalize() 方法的另一个示例。我们使用此方法检索给定字符串 "Hello JavaScript" 的 Unicode 范式,并根据指定的范式值 "NFKC" 修改返回的字符串。
<html>
<head>
<title>JavaScript String normalize() Method</title>
</head>
<body>
<script>
const str = "Hello JavaScript";
document.write("The given string: ", str);
const form = "NFKC";
document.write("<br>The form: ", form);
//使用 normalize() 方法
const new_str = str.normalize(form);
document.write("<br>The Unicode normalization of the given string: ", new_str);
</script>
</body>
</html>
输出
执行上述程序后,将返回一个包含 Unicode 范式的字符串。
The given string: Hello JavaScript The form: NFKC The Unicode normalization of the given string: Hello JavaScript

