Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 846099 of String.prototype.normalize()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/String/normalize
  • Revision title: String.prototype.normalize()
  • Revision id: 846099
  • Created:
  • Creator: leobalter
  • Is current revision? No
  • Comment If the form argument is undefined, normalize will throw a RangeError, as undefined will be converted to "undefined" and it's not a valid form.

Revision Content

{{JSRef}} {{harmony}}

The normalize() method returns the Unicode Normalization Form of a given string (if the value isn't a string, it will be converted to one first).

Syntax

str.normalize([form])

Parameters

form
One of "NFC", "NFD", "NFKC", or "NFKD", specifying the Unicode Normalization Form. If omitted, "NFC" is used.
  • NFC — Normalization Form Canonical Composition.
  • NFD — Normalization Form Canonical Decomposition.
  • NFKC — Normalization Form Compatibility Composition.
  • NFKD — Normalization Form Compatibility Decomposition.

Erros thrown

{{jsxref("RangeError")}}
A {{jsxref("RangeError")}} is thrown if form isn't one of the values specified above.

Description

The normalize() method returns the specified Unicode Normalization Form of the string. It does not affect the value of the string itself.

Examples

Using normalize()

// Initial string

// U+1E9B: LATIN SMALL LETTER LONG S WITH DOT ABOVE
// U+0323: COMBINING DOT BELOW
var 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'

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-string.prototype.normalize', 'String.prototype.normalize')}} {{Spec2('ES6')}} Initial definition.

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatChrome("34")}} {{CompatGeckoDesktop("31")}} {{CompatIE("11")}} {{CompatVersionUnknown}} {{CompatNo}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatChrome("34")}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

See also

Revision Source

<div>{{JSRef}} {{harmony}}</div>

<p>The <strong><code>normalize()</code></strong> method returns the Unicode Normalization Form of a given string (if the value isn't a string, it will be converted to one first).</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
<code><var>str</var>.normalize([<var>form</var>])</code></pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt><code>form</code></dt>
 <dd>One of <code>"NFC"</code>, <code>"NFD"</code>, <code>"NFKC"</code>, or <code>"NFKD"</code>, specifying the Unicode Normalization Form. If omitted<code>, "NFC"</code> is used.
 <ul>
  <li><code>NFC</code>&nbsp;— Normalization Form Canonical Composition.</li>
  <li><code>NFD</code>&nbsp;— Normalization Form Canonical Decomposition.</li>
  <li><code>NFKC</code>&nbsp;— Normalization Form Compatibility Composition.</li>
  <li><code>NFKD</code>&nbsp;— Normalization Form Compatibility Decomposition.</li>
 </ul>
 </dd>
</dl>

<h3 id="Erros_thrown">Erros thrown</h3>

<dl>
 <dt>{{jsxref("RangeError")}}</dt>
 <dd>A {{jsxref("RangeError")}} is thrown if <code>form</code> isn't one of the values specified above.</dd>
</dl>

<h2 id="Description">Description</h2>

<p>The <code>normalize()</code> method returns the specified Unicode Normalization Form of the string. It does not affect the value of the string itself.</p>

<h2 id="Examples">Examples</h2>

<h3 id="Using_normalize()">Using <code>normalize()</code></h3>

<pre class="brush: js">
// Initial string

// U+1E9B: LATIN SMALL LETTER LONG S WITH DOT ABOVE
// U+0323: COMBINING DOT BELOW
var 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'
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-string.prototype.normalize', 'String.prototype.normalize')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatChrome("34")}}</td>
   <td>{{CompatGeckoDesktop("31")}}</td>
   <td>{{CompatIE("11")}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome("34")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="https://www.unicode.org/reports/tr15/">Unicode Standard Annex #15, Unicode Normalization Forms</a></li>
 <li><a href="https://en.wikipedia.org/wiki/Unicode_equivalence">Unicode equivalence</a></li>
</ul>
Revert to this revision