Normalize accented text with normalize
Normalize accented text with String.prototype.normalize, compare composed and decomposed forms, and strip accents with a Unicode property escape. Node.js 18.
'café' can be four characters or five, and the two spellings do not compare equal. normalize() makes them match, and with NFD it lets you strip the accent. The output is from Node.js 24.
const word = 'café';
const decomposed = word.normalize('NFD');
console.log(word.length, decomposed.length);
console.log(word === decomposed, word.normalize('NFC') === decomposed.normalize('NFC'));
console.log(decomposed.replace(/\p{Diacritic}/gu, ''));
console.log('Crème Brûlée'.normalize('NFD').replace(/\p{M}/gu, ''));
console.log('ß'.normalize('NFD'), 'ø'.normalize('NFD').replace(/\p{M}/gu, ''));4 5
false true
cafe
Creme Brulee
ß øUnicode lets an é be one code point or an e followed by a combining accent, so the same word has length 4 or 5, and === says false. normalize('NFC') composes and normalize('NFD') decomposes, so after normalizing both to one form they match, which is the true on the second line. To remove accents, decompose with NFD and delete the combining marks with a regex on the Unicode mark property and the u flag, which turns Crème Brûlée into Creme Brulee. The last line is the limit: ß and ø do not decompose, so they come out unchanged. The forms are defined in the specification for String.prototype.normalize.
Normalize both sides before you compare text from different sources, such as a file name from macOS, which stores decomposed names, and one typed by a user. The Unicode Diacritic property also works in that regex, but it is a different set from the mark property the snippet uses. Accent stripping is lossy and changes meaning in some languages, so use it for search keys, not for stored text.