normalize() method returns the Unicode Normalization Form of the string.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

句法

str.normalize([form])
					

参数

form 可选

One of "NFC" , "NFD" , "NFKC" ,或 "NFKD" , specifying the Unicode Normalization Form. If omitted or undefined , "NFC" 被使用。

These values have the following meanings:

"NFC"

Canonical Decomposition, followed by Canonical Composition.

"NFD"

Canonical Decomposition.

"NFKC"

Compatibility Decomposition, followed by Canonical Composition.

"NFKD"

Compatibility Decomposition.

返回值

A string containing the Unicode Normalization Form of the given string.

Errors thrown

RangeError
RangeError is thrown if form isn't one of the values specified above.

描述

Unicode assigns a unique numerical value, called a code point , to each character. For example, the code point for "A" is given as U+0041. However, sometimes more than one code point, or sequence of code points, can represent the same abstract character — the character "ñ" for example can be represented by either of:

  • The single code point U+00F1.
  • The code point for "n" (U+006E) followed by the code point for the combining tilde (U+0303).
let string1 = '\u00F1';
let string2 = '\u006E\u0303';
console.log(string1);  //  ñ
console.log(string2);  //  ñ
					

However, since the code points are different, string comparison will not treat them as equal. And since the number of code points in each version is different, they even have different lengths.

let string1 = '\u00F1';            // ñ
let string2 = '\u006E\u0303';      // ñ
console.log(string1 === string2); // false
console.log(string1.length);      // 1
console.log(string2.length);      // 2
					

normalize() method helps solve this problem by converting a string into a normalized form common for all sequences of code points that represent the same characters. There are two main normalization forms, one based on canonical equivalence and the other based on compatibility .

Canonical equivalence normalization

In Unicode, two sequences of code points have canonical equivalence if they represent the same abstract characters, and should always have the same visual appearance and behavior (for example, they should always be sorted in the same way).

可以使用 normalize() 使用 "NFD" or "NFC" arguments to produce a form of the string that will be the same for all canonically equivalent strings. In the example below we normalize two representations of the character "ñ" :

let string1 = '\u00F1';           // ñ
let string2 = '\u006E\u0303';     // ñ
string1 = string1.normalize('NFD');
string2 = string2.normalize('NFD');
console.log(string1 === string2); // true
console.log(string1.length);      // 2
console.log(string2.length);      // 2
					

Composed and decomposed forms

Note that the length of the normalized form under "NFD" is 2 . That's because "NFD" gives you the decomposed version of the canonical form, in which single code points are split into multiple combining ones. The decomposed canonical form for "ñ" is "\u006E\u0303" .

You can specify "NFC" 以获取 composed canonical form, in which multiple code points are replaced with single code points where possible. The composed canonical form for "ñ" is "\u00F1" :

let string1 = '\u00F1';                           // ñ
let string2 = '\u006E\u0303';                     // ñ
string1 = string1.normalize('NFC');
string2 = string2.normalize('NFC');
console.log(string1 === string2);                 // true
console.log(string1.length);                      // 1
console.log(string2.length);                      // 1
console.log(string2.codePointAt(0).toString(16)); // f1
					

Compatibility normalization

In Unicode, two sequences of code points are compatible if they represent the same abstract characters, and should be treated alike in some — but not necessarily all — applications.

All canonically equivalent sequences are also compatible, but not vice versa.

例如:

  • the code point U+FB00 represents the ligature "ff" . It is compatible with two consecutive U+0066 code points ( "ff" ).
  • the code point U+24B9 represents the symbol "Ⓓ" . It is compatible with the U+0044 code point ( "D" ).

In some respects (such as sorting) they should be treated as equivalent—and in some (such as visual appearance) they should not, so they are not canonically equivalent.

可以使用 normalize() 使用 "NFKD" or "NFKC" arguments to produce a form of the string that will be the same for all compatible strings:

let string1 = '\uFB00';
let string2 = '\u0066\u0066';
console.log(string1);             // ff
console.log(string2);             // ff
console.log(string1 === string2); // false
console.log(string1.length);      // 1
console.log(string2.length);      // 2
string1 = string1.normalize('NFKD');
string2 = string2.normalize('NFKD');
console.log(string1);             // ff <- visual appearance changed
console.log(string2);             // ff
console.log(string1 === string2); // true
console.log(string1.length);      // 2
console.log(string2.length);      // 2
					

When applying compatibility normalization it's important to consider what you intend to do with the strings, since the normalized form may not be appropriate for all applications. In the example above the normalization is appropriate for search, because it enables a user to find the string by searching for "f" . But it may not be appropriate for display, because the visual representation is different.

As with canonical normalization, you can ask for decomposed or composed compatible forms by passing "NFKD" or "NFKC" , respectively.

范例

使用 normalize()

// Initial string
// U+1E9B: LATIN SMALL LETTER LONG S WITH DOT ABOVE
// U+0323: COMBINING DOT BELOW
let str = '\u1E9B\u0323';
// Canonically-composed form (NFC)
// U+1E9B: LATIN SMALL LETTER LONG S WITH DOT ABOVE
// U+0323: COMBINING DOT BELOW
str.normalize('NFC'); // '\u1E9B\u0323'
str.normalize();      // same as above
// Canonically-decomposed form (NFD)
// U+017F: LATIN SMALL LETTER LONG S
// U+0323: COMBINING DOT BELOW
// U+0307: COMBINING DOT ABOVE
str.normalize('NFD'); // '\u017F\u0323\u0307'
// Compatibly-composed (NFKC)
// U+1E69: LATIN SMALL LETTER S WITH DOT BELOW AND DOT ABOVE
str.normalize('NFKC'); // '\u1E69'
// Compatibly-decomposed (NFKD)
// U+0073: LATIN SMALL LETTER S
// U+0323: COMBINING DOT BELOW
// U+0307: COMBINING DOT ABOVE
str.normalize('NFKD'); // '\u0073\u0323\u0307'
					

规范

规范
ECMAScript (ECMA-262)
The definition of 'String.prototype.normalize' in that specification.

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
normalize Chrome 34 Edge 12 Firefox 31 IE No Opera 21 Safari 10 WebView Android No Chrome Android 34 Firefox Android 31 Opera Android 21 Safari iOS 10 Samsung Internet Android 2.0 nodejs 0.12

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改:
  1. 标准内置对象
  2. String
  3. 特性
    1. String 长度
  4. 方法
    1. String.fromCharCode()
    2. String.fromCodePoint()
    3. String.prototype.anchor()
    4. String.prototype.big()
    5. String.prototype.blink()
    6. String.prototype.bold()
    7. String.prototype.charAt()
    8. String.prototype.charCodeAt()
    9. String.prototype.codePointAt()
    10. String.prototype.concat()
    11. String.prototype.endsWith()
    12. String.prototype.fixed()
    13. String.prototype.fontcolor()
    14. String.prototype.fontsize()
    15. String.prototype.includes()
    16. String.prototype.indexOf()
    17. String.prototype.italics()
    18. String.prototype.lastIndexOf()
    19. String.prototype.link()
    20. String.prototype.localeCompare()
    21. String.prototype.match()
    22. String.prototype.matchAll()
    23. String.prototype.normalize()
    24. String.prototype.padEnd()
    25. String.prototype.padStart()
    26. String.prototype.repeat()
    27. String.prototype.replace()
    28. String.prototype.replaceAll()
    29. String.prototype.search()
    30. String.prototype.slice()
    31. String.prototype.small()
    32. String.prototype.split()
    33. String.prototype.startsWith()
    34. String.prototype.strike()
    35. String.prototype.sub()
    36. String.prototype.substr()
    37. String.prototype.substring()
    38. String.prototype.sup()
    39. String.prototype.toLocaleLowerCase()
    40. String.prototype.toLocaleUpperCase()
    41. String.prototype.toLowerCase()
    42. String.prototype.toSource()
    43. String.prototype.toString()
    44. String.prototype.toUpperCase()
    45. String.prototype.trim()
    46. String.prototype.trimEnd()
    47. String.prototype.trimStart()
    48. String.prototype.valueOf()
    49. String.prototype[@@iterator]()
    50. String.raw()
  5. 继承:
  6. Function
  7. 特性
    1. Function.arguments
    2. Function.caller
    3. Function.displayName
    4. Function.length
    5. Function.name
  8. 方法
    1. Function.prototype.apply()
    2. Function.prototype.bind()
    3. Function.prototype.call()
    4. Function.prototype.toSource()
    5. Function.prototype.toString()
  9. Object
  10. 特性
    1. Object.prototype.__proto__
    2. Object.prototype.constructor
  11. 方法
    1. Object.prototype.__defineGetter__()
    2. Object.prototype.__defineSetter__()
    3. Object.prototype.__lookupGetter__()
    4. Object.prototype.__lookupSetter__()
    5. Object.prototype.hasOwnProperty()
    6. Object.prototype.isPrototypeOf()
    7. Object.prototype.propertyIsEnumerable()
    8. Object.prototype.toLocaleString()
    9. Object.prototype.toSource()
    10. Object.prototype.toString()
    11. Object.prototype.valueOf()
    12. Object.setPrototypeOf()