30秒学会 JavaScript 片段 · 2023年7月16日

30秒学会 JavaScript 片段 – Remove accents

Removes accents from strings.

  • Use String.prototype.normalize() to convert the string to a normalized Unicode format.
  • Use String.prototype.replace() to replace diacritical marks in the given Unicode range by empty strings.

代码实现

const removeAccents = str =>
  str.normalize('NFD').replace(/[\u0300-\u036f]/g, '');

removeAccents('Antoine de Saint-Exupéry'); // 'Antoine de Saint-Exupery'

翻译自:https://www.30secondsofcode.org/js/s/remove-accents