Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Normalize all UTF quotes in Javascript
/**
* Will normalize quotes in a given string. There are many variations of quotes
* in the unicode character set, this function attempts to convert any variation
* of quote to the standard Quotation Mark - U+0022 Standard Universal: "
*
* @param {string} str The string to normalize
* @return {string} Normalized string.
* @see https://unicode-table.com/en/sets/quotation-marks/
*/
helpers.stdQuote = (str) => {
const allQuotes = [
'“', // U+201c
'”', // U+201d
'«', // U+00AB
'»', // U+00BB
'„', // U+201E
'“', // U+201C
'‟', // U+201F
'”', // U+201D
'❝', // U+275D
'❞', // U+275E
'〝', // U+301D
'〞', // U+301E
'〟', // U+301F
'"', // U+FF02
];
const stdQuote = '"'; // U+0022
const normalized = allQuotes.reduce((strNorm, quoteChar) => {
// eslint-disable-next-line security/detect-non-literal-regexp
const re = new RegExp(quoteChar, 'g');
return strNorm.replace(re, stdQuote);
}, str);
return normalized;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment