Skip to content

Instantly share code, notes, and snippets.

@kjantzer
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save kjantzer/503e46328ff692bfe38b to your computer and use it in GitHub Desktop.

Select an option

Save kjantzer/503e46328ff692bfe38b to your computer and use it in GitHub Desktop.
Smart Quotes – changes straight double and single quotes to curly, double dashes to em-dashes, and triple periods to ellipsis.
// http://stackoverflow.com/a/14890774/484780
smartQuotes = function(str){
return str = (str||'').replace(/\b'\b/g, "\u2019") // apostrophes
.replace(/'(?=[^>]*<)\b/g, "\u2018") // Opening singles
.replace(/\b([\.\?\!,]*)(?=[^>]*<)'/g, "$1\u2019") // Closing singles
.replace(/"(?=[^>]*<)\b/g, "\u201c") // Opening doubles
.replace(/\b([\.\?\!,]*)(?=[^>]*<)"/g, "$1\u201d") // Closing doubles
.replace(/\.\.\./g, "\u2026") // ellipsis
.replace(/--/g, "\u2014") // em-dashes
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment