23

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?

2

7 Answers 7

15

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, "“");
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)

7
  • +1 for actually answering the question. Though users of this should take into account that it's not perfect in every situation - for example, the dashes indicating feet and inches.
    – Anon.
    Feb 4, 2010 at 20:15
  • ... or punctuation that ends a quotation.
    – Pointy
    Feb 4, 2010 at 20:20
  • Thank you! This is what I was looking for. One note, copying the code exactly gave me an error. the '?<=' portion was changed to '?='. Also, I had to remove the quotation character from the end case for it to match correctly. The code: s = s.replace(/"(?=\w|$)/g, "“"); s = s.replace(/(?=[\w,.?!\-")]|^)"/g, "”");
    – BlueVoid
    Feb 4, 2010 at 20:33
  • @BlueVoid - You are correct about the error, I discovered this and was updating my answer as you commented :) Be careful with your code - ?= is a look-ahead, which matches because it looks ahead and sees the quote, which is in your character class. I would go with the first "alternative" solution in my edited answer -- just replace all of them with right curly quotes after replacing the left curly quotes.
    – Nicole
    Feb 4, 2010 at 20:37
  • @Renesis Good point. This simplifies things anyway. It's working great.
    – BlueVoid
    Feb 4, 2010 at 20:44
5

You might want to look at what Pandoc does—apparently with the --smart option, it handles quotes properly in all cases (including e.g. ’tis and ’twere).

I recently wrote a Javascript typography prettification engine that does, among other things, quote replacement; I wound up using basically the algorithm suggested by Renesis, but there’s currently a failing test up waiting for a smarter solution.

If you’re interested in cribbing my code (and/or submitting a patch based on work you’ve done), check it out: jsPrettify. jsprettify.prettifyStr does what you’re looking for. If you don’t want to deal with the Closure dependency, there’s an older version that runs on its own—it even works in Rhino.

1
  • Plus 1 for Pandoc. I try to use a mature and tested tool whenever I can versus baking my own regex. Hand built regex's won't can be overly greedy, or not greedy enough, and they may not be sensitive to word boundary's and comma's etc. Pandoc accounts for most this and more.
    – Paulb
    Feb 27, 2016 at 14:21
4
'foo "foo bar" "bar"'.replace(/"([-a-zA-Z0-9 ]+)"/g, function(wholeMatch, m1){
    return "“" + m1 + "”";
});
8
  • 1
    This does not answer the question.
    – SLaks
    Feb 4, 2010 at 20:13
  • 1
    How would it handle "John was 6' 4""?
    – Anon.
    Feb 4, 2010 at 20:18
  • 1
    @Anon, how would any program handle that without being smart enough to know what inches and feet are?
    – Earlz
    Feb 4, 2010 at 20:24
  • 2
    Stuff I've seen normally curlies the quote mark in the correct direction, but also curlies the inch mark the same way. What it doesn't do is curly the inch mark, and then curly every single other quote mark the opposite way from what it's supposed to do.
    – Anon.
    Feb 4, 2010 at 20:27
  • 1
    @Anon: then maybe you should post a solution. Feb 4, 2010 at 20:34
3

The following just changes every quote by alternating (this specific example however would leave out the orphaned quotes).

str.replace(/\"([^\"]*)\"/gi,"&#8220;$1&#8221;");

Works perfectly, as long as the text you're texturizing isn't already screwed up with improper use of the double quote. In English, quotes are never nested.

1
  • 4
    There is one legitimate situation in English where this rule breaks down. When you have consecutive paragraphs representing quoted speech by the same speaker, one must start each of those paragraphs with the appropriate quote marks (single, double, single+double, double+single, etc), but one omits the closing quote except for the last paragraph by the same speaker.
    – tchrist
    Nov 29, 2010 at 5:17
0

I don't think something like that in general is easy at all, because you'd have to interpret exactly what each double-quote character in your content means. That said, what I'd do is collect all the text nodes I was interested in, and then go through and keep track of the "on/off" (or "odd/even"; whatever) nature of each double quote instance. Then you can know which replacement entity to use.

0

I didn't find the logic I wanted here, so here's what I ended up going with.

value = value.replace(/(^|\s)(")/g, "$1“"); // replace quotes that start a line or follow spaces
value = value.replace(/"/g, "”"); // replace rest of quotes with the back smart quote

I have a small textarea that I need to replace straight quotes with curly (smart) quotes. I'm just executing this logic on keyup. I tried to make it behave like Microsoft Word.

0

Posting for posterity.

As suggested by @Steven Dee, I went to Pandoc.

I try to use a mature and tested tool whenever I can versus baking my own regex. Hand built regex's can be overly greedy, or not greedy enough, and they may not be sensitive to word boundaries and commas etc. Pandoc accounts for most this and more.

From the command line (the --smart parameter turns on smart quotes):

pandoc --smart --standalone -o output.html input.html

..and I know a command line script may or may not fit OP's requirement of using Javascript. (related: How to execute shell command in Javascript)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.