Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting ″Straight Quotes″ to “Curly Quotes”

Tags:

I have an application which uses a Javascript-based rules engine. I need a way to convert regular straight quotes into curly (or smart) quotes. It’d be easy to just do a string.replace for ["], only this will only insert one case of the curly quote.

The best way I could think of was to replace the first occurrence of a quote with a left curly quote and every other one following with a left, and the rest right curly.

Is there a way to accomplish this using Javascript?

like image 795
BlueVoid Avatar asked Feb 04 '10 20:02

BlueVoid


People also ask

How do you make a quote Curly?

For the curly single opening and closing quote mark (or apostrophe), use ‘ and ’ respectively. For the curly opening and closing double quotation marks, use “ and ” respectively.

How do you change straight quotes to curly in Excel?

In a document that already has straight quotes (I assume that is where you are seeing the problem), do a find and replace: Find: single or double quote (' or " - use the actual character) and Replace: same type of quote as in Find (' or "). This will replace the straight quotes with curly (smart) quotes.

How do you change straight quotes to curly quotes in Google Docs?

If you want all your quotation marks and apostrophes to be curly after you use Text Cleaner, go to Tools > Preferences and check the box under "General" for "User smart quotes." Then press the blue OK button to save your changes.


1 Answers

You could replace all that preceed a word character with the left quote, and all that follow a word character with a right quote.

str = str.replace(/"(?=\w|$)/g, "&#8220;"); str = str.replace(/(?<=\w|^)"/g, "&#8221;"); // IF the language supports look-                                              // behind. Otherwise, see below. 

As pointed out in the comments below, this doesn't take punctuation into account, but easily can:

/(?<=[\w,.?!\)]|^)"/g 

[Edit:] For languages that don't support look-behind, like Javascript, as long as you replace all the front-facing ones first, you have two options:

str = str.replace(/"/g, "&#8221;"); // Replace the rest with right curly quotes // or... str = str.replace(/\b"/g, "&#8221;"); // Replace any quotes after a word                                       // boundary with right curly quotes 

(I've left the original solution above in case this is helpful to someone using a language that does support look-behind)

like image 144
Nicole Avatar answered Oct 29 '22 01:10

Nicole