简体   繁体   English

将哑引号转换为智能引号 Javascript 问题

[英]Dumb quotes into smart quotes Javascript issue

I have some JavaScript code that transforms dumb quotes into smart quotes in a contenteditable .我有一些 JavaScript 代码可以在contenteditable中将哑引号转换为智能引号。

The problem appears when you add dumb quotes at the beginning of the line they only close.当您在仅关闭的行的开头添加哑引号时会出现问题。 For example you get this:例如你得到这个:

”dumb quotes” instead of “dumb quotes”

Try out the demo: http://jsfiddle.net/7rcF2/试用演示: http : //jsfiddle.net/7rcF2/

The code I'm using:我正在使用的代码:

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  };

Any ideas?有任何想法吗? Thanks!谢谢!

PS I suck at regular expressions… PS我很讨厌正则表达式......

Try this:试试这个:

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.  

Output:输出:

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

Basically, you were looking for the word boundary : \\b基本上,您正在寻找单词边界\\b

Here's an updated fiddle这是一个更新的小提琴

If you want everything done client side, you can use smartquotes.js library to convert all dumb quotes on the page to smart quotes.如果您希望在客户端完成所有工作,您可以使用smartquotes.js库将页面上的所有哑引号转换为智能引号。 Alternatively, you can use the regex from the library itself .或者,您可以使用库本身正则表达式

Here's an implementation from an old version of the code:这是旧版本代码的实现:

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');
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM