Skip to content

Instantly share code, notes, and snippets.

@qgustavor
Last active August 8, 2020 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qgustavor/9c373ca8719eaacbbe912793f08fdb2d to your computer and use it in GitHub Desktop.
Save qgustavor/9c373ca8719eaacbbe912793f08fdb2d to your computer and use it in GitHub Desktop.
Swap quotes to handle quotation of titles already containing quotes.
function swapQuotes (input) {
const quotesStr = `'"“‘”’`
return input.replace(/['"“”‘’]/g, e => quotesStr.charAt(quotesStr.indexOf(e) ^ 1))
}
// Example usage:
// const episodeName = 'Marge Simpson in: "Screaming Yellow Honkers"'
// <div>This user watched "{{ swapQuotes(episodeName) }}" in...</div>
// Should return <div>This user watched "Marge Simpson in: 'Screaming Yellow Honkers'" in...</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment