JavaScript Reference · String
String.prototype.normalize()
Reference for JavaScript String.prototype.normalize(): convert text to NFC, NFD, NFKC or NFKD so equivalent strings compare equal, and strip accents safely.
The normalize() method returns a string converted to a Unicode normalization form. It solves a surprising problem: the same visible text can be stored as different sequences of code points, and === treats them as different strings.
The letter é, for example, can be one code point (U+00E9) or an e followed by a combining acute accent (U+0301). They look identical, but their lengths differ. Normalizing both to the same form makes them equal.
"é".normalize() === "é"; // true
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
string.normalize() string.normalize(form)
| Name | Description |
|---|---|
form |
Optional. One of "NFC" (default), "NFD", "NFKC" or "NFKD". undefined means "NFC". |
Return value: a new string in the requested form.
Exceptions: a RangeError if form is anything else.
Usage notes
- NFC composes: it combines base letters and accents into single code points where possible. It is the best choice for storing and comparing text, and it is what the web mostly uses.
- NFD decomposes: it splits characters into a base letter plus combining marks. That makes removing or inspecting accents easy.
- NFKC and NFKD also apply "compatibility" mappings: ligatures like
fibecomefi, circled digits become plain digits, and full-width letters become ASCII. This loses information, so use them for searching or matching, not for text you must display unchanged. - Compare normalized strings when text comes from different sources, such as user input, filenames from different operating systems, or a database. A search for
caféshould find both encodings. - Removing accents with
normalize("NFD")and a regex for the mark category\p{M}, as above, works for many Latin-script words. It is not correct for every language: it turns Hangul syllables into parts and leaves letters likeøandłuntouched. - It does not handle case.
"É".normalize()is still uppercase; combine withtoLowerCase()when needed. - Length changes. Normalizing can change
length, which matters for limits on text length. localeCompare()already treats canonically equivalent strings as equal, which is why sorting usually works without normalizing first. SeelocaleCompare().- What the user sees as one character can still be several code points after normalization (emoji sequences, for example). See strings and Unicode.
Browser support
normalize() is part of ES2015 and works in all current browsers.