Skip to content

Commit dedf7e2

Browse files
authored
[Term Entry] JavaScript Strings: .normalize()
* creat folder normalize and file normalize.md with all information about it * minor content fixes * Update normalize.md * Minor changes ---------
1 parent 6a7b494 commit dedf7e2

File tree

1 file changed

+65
-0
lines changed
  • content/javascript/concepts/strings/terms/normalize

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
Title: '.normalize()'
3+
Description: 'Returns the Unicode Normalization Form of a string.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
Tags:
8+
- 'JavaScript'
9+
- 'Methods'
10+
- 'String'
11+
- 'Unicode'
12+
CatalogContent:
13+
- 'introduction-to-javascript'
14+
- 'paths/front-end-engineer-career-path'
15+
---
16+
17+
The **`.normalize()`** method in JavaScript returns the Unicode Normalization Form of a string. This is especially useful when comparing strings that may look identical but are composed of different Unicode code points.
18+
19+
## Syntax
20+
21+
```pseudo
22+
string.normalize([form])
23+
```
24+
25+
**Parameters:**
26+
27+
- `form` (Optional): A string specifying the Unicode normalization form. Valid values are:
28+
- `'NFC'` (Default): Canonical Composition
29+
- `'NFD'`: Canonical Decomposition
30+
- `'NFKC'`: Compatibility Composition
31+
- `'NFKD'`: Compatibility Decomposition
32+
33+
**Return value:**
34+
35+
A new string in the specified normalization form.
36+
37+
## Example: Comparing Unicode Representations
38+
39+
In this example, two visually identical strings have different Unicode encodings, and `.normalize()` is used to make them comparable:
40+
41+
```js
42+
const word1 = '\u00e9'; // é as single character
43+
const word2 = '\u0065\u0301'; // e + ́ (combining acute)
44+
45+
console.log(word1 === word2);
46+
console.log(word1.normalize() === word2.normalize());
47+
```
48+
49+
The output of this code is:
50+
51+
```shell
52+
false
53+
true
54+
```
55+
56+
## Codebyte Example: Stripping Accents Using Normalize + Regex
57+
58+
In this codebyte example, `.normalize()` is combined with a regular expression to strip accents by removing Unicode diacritical marks:
59+
60+
```codebyte/javascript
61+
const word = 'café';
62+
const stripped = word.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
63+
64+
console.log(stripped);
65+
```

0 commit comments

Comments
 (0)