# Quote my quote!

A quotation is "the repetition of one expression as part of another one". In American English, a quotation is surrounded by double-quotation marks ("), as in the previous sentence. However, this rule differs when a quote appears inside another quote. In this case, the inner quote is surrounded by single-quotation marks (').

# Challenge

Given a string as input, output a quotable version of that string by exchanging double- and single-quotation marks that are used for inner quotation.

There's just one catch, though - not all single-quotation marks are used for quotation! For this challenge, single-quotation mark is used for quotation if and only if:

• The mark is preceded by a non-alphanumeric character, or
• The mark is followed by a non-alphanumeric character that is not a space

The marks used for quotations are guaranteed to be balanced; that is, you do not need to handle invalid input. Additionally, double-quotation marks will only be used in situations where single-quotation marks can be used to represent quotation.

# Test Cases

A man, a plan, a canal, Panama.
"A man, a plan, a canal, Panama."

Socrates once said, "An unexamined life is not worth living."
"Socrates once said, 'An unexamined life is not worth living.'"

@musicman523 said, "Socrates once said, 'An unexamined life is not worth living.'"
"@musicman523 said, 'Socrates once said, "An unexamined life is not worth living."'"

"Nested 'quotes "can 'cause "recursion," if,' it," runs,' deep!"
"'Nested "quotes 'can "cause 'recursion,' if," it,' runs," deep!'"

"Contractions' behaviors can't be easy to account for," he warned.
"'Contractions' behaviors can't be easy to account for,' he warned."

@carusocomputing said, "Nested, 'quotes, it's unfortunate', to be sure."
"@carusocomputing said, 'Nested, "quotes, it's unfortunate", to be sure.'"


# Scoring

This is , so the shortest answer in each language wins!

• Sandbox – musicman523 Jun 13 '17 at 0:30
• ... if' it" runs' deep" has single-quotes that are not preceded by a non-alphanumeric, nor are they followed by "non-alphanumeric character that is not a space", and yet they are being transformed! – Value Ink Jun 13 '17 at 0:45
• Personally I don't feel that handling apostrophes adds much of value to the challenge. – ATaco Jun 13 '17 at 0:48
• @JonathanAllan Yes. I think it's difficult to determine whether those single-quotation marks are used for quotation or possession without deeply complicating the rules. I think that would make a decent challenge on its own. – musicman523 Jun 13 '17 at 1:19
• "Nested, 'quotes, it's unfortunate', to be sure." - I think you need a nested case with contractions. – Magic Octopus Urn Jun 13 '17 at 13:52

# Ruby, 646665 64+1 = 656766 65 bytes

+1 byte for the -n flag.

gsub(/"|(?<!\w)'|'(?!\w|\s)/){"'\""["\"'".index$&]}$><<?"+$_+?"  Try it online! • Doesn't seem to do the right thing for single-quotation marks preceded by a space (which are indeed used for quotation) – musicman523 Jun 13 '17 at 1:08 • @musicman523 thanks, fixed! – Value Ink Jun 13 '17 at 1:16 # Retina, 34 27 bytes -7 bytes thanks to @Leo T'""'\W'|"|'[^\w ] ^|$
"


Try it online!

• With @carusocomputing said, "Nested, 'quotes, it's unfortunate', to be sure." as the input, the single-quote following unfortunate is not changed to a double-quote, but should be. If it helps you debug, it works when the comma comes before the quote. (I don't know Retina so I can't really help any further.) – musicman523 Jun 13 '17 at 14:14
• @musicman523 fixed it – ovs Jun 13 '17 at 14:34
• You can use \w directly inside your character class – Leo Jun 13 '17 at 14:36

# JavaScript (ES6), 90 bytes

Assumes the string doesn't contain #.

s=>'"'+s[r='replace'](/"/g,'#')[r](/(\W)'/g,'$1"')[r](/'([^\w ])/g,'"$1')[r](/#/g,"'")+'"'


f=
s=>'"'+s[r='replace'](/"/g,'#')[r](/(\W)'/g,'$1"')[r](/'([^\w ])/g,'"$1')[r](/#/g,"'")+'"'
;

A man, a plan, a canal, Panama.
Socrates once said, "An unexamined life is not worth living."
@musicman523 said, "Socrates once said, 'An unexamined life is not worth living.'"
"Nested 'quotes "can 'cause "recursion," if,' it," runs,' deep!"
"Contractions' behaviors can't be easy to account for," he warned.
@carusocomputing said, "Nested, 'quotes, it's unfortunate', to be sure..split(\n).map(f).map(s=>console.log(s))`