Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

396
Views
How to get the accent/diacritic of a letter in javascript?

I want to get the accent/diacritic of a letter in javascript.

For example:

  • ñ -> ~
  • á -> ´
  • è -> `

I tried using .normalize("NFD") but it doesn't return the correct accent/diacritc

string = "á"
string.normalize("NFD").split("")
// ['a', '́']
string.normalize("NFD").split("").includes("´") 
// false
'́' === "´"
// false

I want NFD or any other function to give the accent/diacritic instead of the combining accent/diacritic

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Your method to get the accent/diacritic of a letter is correct by using the string.normalize("NFD").split("").

The normalize("NFD") returns the correct result which in this case is the Combining Acute Accent Unicode Decimal Code &#769.

However, what you are doing is comparing the output for the letter á from the normalize("NFD") which is the Combining Acute Accent (char code 769) with the Normal Acute Accent (char code 180). Of course, these are 2 different letters.

The same applies to the letter è which has the Combining Grave Accent (char code 768); and you are comparing it to the Normal Grave Accent (char code 96) which we use and type from the keyboard; they are 2 different letters.

The standalone (normal) letters (including the Acute Accent and the Grave Accent letters) will always be separate letters even if they come before or after any other letters in a string. However, the combining forms of the letters (which have different chars codes to distinguish them) will go above or below the next or previous letter they are adjacent to. This is similar in Arabic accent letters and other languages like Hebrew.

Here are comparisons of some of the tilde letters:

enter image description here

See below the examples:

console.log("á".normalize("NFD").split("")[1].charCodeAt()); // 769 code for Combining Acute Accent
console.log("´".charCodeAt());                               // 180 code for Normal Acute Accent

console.log("è".normalize("NFD").split("")[1].charCodeAt()); // 768 Combining Grave Accent
console.log("`".charCodeAt());                               // 96 Normal Grave Accent

console.log("á".normalize("NFD").split("")[1].charCodeAt()); // 769 code for Combining Acute Accent
console.log("´".charCodeAt());                               // 180 code for Normal Acute Accent
    
console.log("è".normalize("NFD").split("")[1].charCodeAt()); // 768 Combining Grave Accent
console.log("`".charCodeAt());                               // 96 Normal Grave Accent

over 4 years ago · Santiago Trujillo Report

0

The short answer is because COMBINING TILDE != TILDE

Here's a breakdown of each of the Unicode characters potentially involved in ñ for example:

Symbol Code CodePoint Name
ñ \u00F1 241 LATIN SMALL LETTER N WITH TILDE
n \u006E 110 LATIN SMALL LETTER N
̃ \u0303 771 COMBINING TILDE
~ \u007E 126 TILDE

In order to be able to separate out the diacritical marks from their attached characters, you can use string.normalize with "NFD" which provides the "Canonical Decomposition", breaking up a single glyph into different character combinations that result in the same symbol.

There are 112 different combining diacritical marks. I can't find a native way to convert between the combining character and it's solo counterpart. You could look for a library or write the mapping yourself for marks you want to handle like this:

const combiningMarks = {
  771: 126, // tilde
  769: 180, // acute accent
  768: 96,  // grave accent
}

Then decompose to separate chars and lookup the associated mark for each combining char like this:

const combiningMarks = {
  771: 126, // tilde
  769: 180, // acute accent
  768: 96,  // grave accent
}

const startingString = "ñáè" // "\u00F1\u00E1\u00E8"
const decomposedString = startingString.normalize("NFD") // "\u006E\u0303\u0061\u0301\u0065\u0300"
const codepoints = [...decomposedString].map(c => c.codePointAt(0)) // [110, 771, 97, 769, 101, 768]
const charsWithFullMarks = codepoints.map(c => combiningMarks[c] || c) // [110, 126, 97, 180, 101, 96]
const finalString = String.fromCodePoint(...charsWithFullMarks) // "n~a´e`"
console.log(finalString);

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!