Skip to content

Instantly share code, notes, and snippets.

@everdimension
Created October 18, 2018 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save everdimension/6995ef906a742c58f76e3abc7a97b6e5 to your computer and use it in GitHub Desktop.
Save everdimension/6995ef906a742c58f76e3abc7a97b6e5 to your computer and use it in GitHub Desktop.
Detect occurrence of a string in another string by normalizing both strings and ignoring diacritical marks
function normalizedIncludes(str1: string, str2: string) {
/**
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize
*
* https://stackoverflow.com/a/37511463/3523645
*/
const diacriticsRange = /[\u0300-\u036f]/g;
const str1Normalized = str1.normalize('NFD').replace(diacriticsRange, '');
const str2Normalized = str2.normalize('NFD').replace(diacriticsRange, '');
return str1Normalized.includes(str2Normalized);
}
/**
* Usage:
* normalizedIncludes('Crème Brulée', 'Creme'); // output: true
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment