Skip to content

Instantly share code, notes, and snippets.

@jesugmz
Created March 6, 2018 22:35
Show Gist options
  • Save jesugmz/c933ac66ae39e36da2e63539fefd01a9 to your computer and use it in GitHub Desktop.
Save jesugmz/c933ac66ae39e36da2e63539fefd01a9 to your computer and use it in GitHub Desktop.
Normalize characters in JavaScript
// replace á per a; ñ per n etc
normalizeDiacriticalMarks = (string) => {
  return string.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
}

// remove all character are not hypen minus ("-"), space or in the range a-z
sanitize = (string) => {
  string = this.normalizeDiacriticalMarks(string);
  return string.replace(/[^\u002D\u0020\u0061-\u007A]/gi, '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment