【问题标题】:Dumb quotes into smart quotes Javascript issue将哑引号转换为智能引号 Javascript 问题
【发布时间】:2013-01-31 04:34:41
【问题描述】:

我有一些 JavaScript 代码可以将 contenteditable 中的哑引号转换为智能引号。

当您在仅关闭的行的开头添加哑引号时,就会出现问题。例如你得到这个:

”dumb quotes” instead of “dumb quotes”

试用演示:http://jsfiddle.net/7rcF2/

我正在使用的代码:

function replace(a) {
    a = a.replace(/(^|[-\u2014\s(\["])'/g, "$1\u2018");       // opening singles
    a = a.replace(/'/g, "\u2019");                            // closing singles & apostrophes
    a = a.replace(/(^|[-\u2014/\[(\u2018\s])"/g, "$1\u201c"); // opening doubles
    a = a.replace(/"/g, "\u201d");                            // closing doubles
    a = a.replace(/--/g, "\u2014");                           // em-dashes
return a  };

有什么想法吗?谢谢!

附注我很讨厌正则表达式……

【问题讨论】:

标签: javascript html replace quotes


【解决方案1】:

试试这个:

var a = '"dumb quotes" instead -- of "dumb quotes", fixed it\'s';

 a = a.replace(/'\b/g, "\u2018")     // Opening singles
      .replace(/\b'/g, "\u2019")     // Closing singles
      .replace(/"\b/g, "\u201c")     // Opening doubles
      .replace(/\b"/g, "\u201d")     // Closing doubles
      .replace(/--/g,  "\u2014")     // em-dashes
      .replace(/\b\u2018\b/g,  "'"); // And things like "it's" back to normal.
// Note the missing `;` in these lines. I'm chaining the `.replace()` functions.  

输出:

'“dumb quotes” instead — of “dumb quotes”, fixed it's'

基本上,您正在寻找word boundary\b

Here's an updated fiddle

【讨论】:

  • 这根本不是一个很好的解决方案。它也将撇号转换为“it's”之类的东西。
  • 值得注意的是,我正在使用 actionscript 正则表达式库,它显然有一个(可能)废话实现 \b
  • @FakeName, it's 标点符号不正确。 @Cerbrus 将它们转换为 it’s 是正确的——参见 practicaltypography.com/straight-and-curly-quotes.html
  • @KevinJantzer - 这不是不正确,使用花引号只会让它看起来更好一点。
  • @KevinJantzer:您的编辑超出了问题的范围。 OP 没有提到在任何地方的 contenteditable 中使用 HTML。如果您觉得这个问题确实需要一个不替换 HTML 标记内引号的答案(并且没有损坏),您应该添加自己的答案。
【解决方案2】:

如果您希望一切都在客户端完成,您可以使用smartquotes.js 库将页面上的所有哑引号转换为智能引号。或者,您可以使用regex from the library itself

这是旧版本代码的实现:

function smartquotesString(str) {
  return str
  .replace(/'''/g, '\u2034')                                                   // triple prime
  .replace(/(\W|^)"(\S)/g, '$1\u201c$2')                                       // beginning "
  .replace(/(\u201c[^"]*)"([^"]*$|[^\u201c"]*\u201c)/g, '$1\u201d$2')          // ending "
  .replace(/([^0-9])"/g,'$1\u201d')                                            // remaining " at end of word
  .replace(/''/g, '\u2033')                                                    // double prime
  .replace(/(\W|^)'(\S)/g, '$1\u2018$2')                                       // beginning '
  .replace(/([a-z])'([a-z])/ig, '$1\u2019$2')                                  // conjunction's possession
  .replace(/((\u2018[^']*)|[a-z])'([^0-9]|$)/ig, '$1\u2019$3')                 // ending '
  .replace(/(\u2018)([0-9]{2}[^\u2019]*)(\u2018([^0-9]|$)|$|\u2019[a-z])/ig, '\u2019$2$3')     // abbrev. years like '93
  .replace(/(\B|^)\u2018(?=([^\u2019]*\u2019\b)*([^\u2019\u2018]*\W[\u2019\u2018]\b|[^\u2019\u2018]*$))/ig, '$1\u2019') // backwards apostrophe
  .replace(/'/g, '\u2032');
};

【讨论】:

  • 英尺/英寸或纬度/经度怎么样,例如,"He's 5'11" tall," she said.
  • @Dan 这不是 JavaScript 中的有效字符串。如果您需要替换非终止引号字符或在替换之前提前转义它们,则可以使用反引号。
  • 是的,我正在使用反引号。我只是写了实际的字符串内容。 `"He's 5'11" tall," she said.`
猜你喜欢
  • 1970-01-01
  • 2013-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-12
  • 1970-01-01
  • 2011-08-30
  • 2010-10-14
相关资源
最近更新 更多