Converting ″Straight Quotes″ to “Curly Quotes”

后端 未结 7 1075
旧时难觅i
旧时难觅i 2020-12-13 19:53

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 <

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 20:21

    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.

提交回复
热议问题