I am trying to replace curly quotes:
str = '“I don’t know what you mean by ‘glory,’ ” Alice said.';
Using:
str.replace(/['"]/g,'');
Why it does not work? How can I do this?
You might have to (or prefer to) use Unicode escapes:
var goodQuotes = badQuotes.replace(/[\u2018\u2019]/g, "'");
That's for funny single quotes; the codes for double quotes are 201C and 201D.
edit — thus to completely replace all the fancy quotes:
var goodQuotes = badQuotes
.replace(/[\u2018\u2019]/g, "'")
.replace(/[\u201C\u201D]/g, '"');
To account for every variation, per https://symbl.cc/en/unicode-table/#general-punctuation :
var badquotes = 'Replace smart quotes with regular quotes: 2018:’2019:‘201C:”201D:“201A:‚201B:‛ 201E:„201F:‟2032:′2033:″2034:‴2035:‵2036:‶2037:‷';
var goodQuotes = badquotes
.replace(/[\u2018\u2019\u201a\u201b\u2032\u2035]/g, "'")
.replace(/[\u201C\u201D\u201E\u201F\u2033\u2034\u2036\u2037]/g, '"');
// Will result in Replace smart quotes with regular quotes: 2018:'2019:'201C:"201D:"201A:'201B:' 201E:"201F:"2032:'2033:"2034:"2035:'2036:"2037:"
.replace(). I'll update the answer..replace(/[\u2013\u2014]/g, '-') .replace(/[\u2026]/g, '...')Combining Pointy and Wooble's answers, if you want to replace the curly quotes with regular quotes:
str = str.replace(/[“”]/g, '"').replace(/[‘’]/g, "'");
So for example:
str = '“I don’t know what you mean by ‘glory,’ ” Alice said.';
str = str.replace(/[“”]/g, '"').replace(/[‘’]/g, "'");
console.log(str)