Created
October 18, 2018 16:45
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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