Skip to content

Instantly share code, notes, and snippets.

@DJTB
Last active May 22, 2016 07:11
Show Gist options
  • Save DJTB/0fb11fb2261b723763a36052f6ce0c6f to your computer and use it in GitHub Desktop.
Save DJTB/0fb11fb2261b723763a36052f6ce0c6f to your computer and use it in GitHub Desktop.
// replace dumb quotes like "these" with “these”
function smartQuotes(str) {
return str
.replace(/'''/g, '\u2034') // triple prime
.replace(/(\W|^)"(\S)/g, '$1\u201c$2') // beginning "
.replace(/(\u201c[^"]*)"([^"]*$|[^\u201c"]*\u201c)/g, '$1\u201d$2') // ending "
.replace(/([^0-9])"/g,'$1\u201d') // remaining " at end of word
.replace(/''/g, '\u2033') // double prime
.replace(/(\W|^)'(\S)/g, '$1\u2018$2') // beginning '
.replace(/([a-z])'([a-z])/ig, '$1\u2019$2') // conjunction's possession
.replace(/((\u2018[^']*)|[a-z])'([^0-9]|$)/ig, '$1\u2019$3') // ending '
// backwards apostrophe
.replace(/(\B|^)\u2018(?=([^\u2019]*\u2019\b)*([^\u2019\u2018]*\W[\u2019\u2018]\b|[^\u2019\u2018]*$))/ig, '$1\u2019')
// abbrev. years like '93
.replace(/(\u2018)([0-9]{2}[^\u2019]*)(\u2018([^0-9]|$)|$|\u2019[a-z])/ig, '\u2019$2$3')
.replace(/'/g, '\u2032');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment