本文介绍了哑引号成聪明的引号Javascript问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一些JavaScript代码可以在 contenteditable 中将愚蠢的引号转换为智能引号。 当你在他们只关闭的行的开头添加哑引号时,会出现问题。例如,你得到这个: 哑引号而不是哑引号 试用演示: http:// jsfiddle.net/7rcF2/ 我正在使用的代码: 函数replace(a){a = a.replace(/(^ | [-\2014 \s(\ [])'/ g,$ 1\202018 ); //打开单曲a = a.replace(/'/ g,\\\’); //关闭单曲和撇号a = a.replace(/(^ | [-\\ \2014 \\\ [(\\\‘\s))/ g,$ 1\\\); //打开双打a = a.replace(// g,\\\ ); //关闭双打a = a.replace(/ - / g,\\\—); // em-dashes return a}; 任何想法?谢谢! PS我吸食正则表达式... 解方案 试试这个: var a ='dumb quotesinstead - ofdumb quotes ,修好它的'; a = a.replace(/'\b / g,\\\‘)//打开单曲 .replace(/ \b'/ g,\\\’ )//关闭单打 .replace(/\b / g,\\\)//打开双打 .replace(/ \b/ g,\\\ )//关闭双打 .replace(/ - / g,\\\—)// em-dashes .replace(/ \\\\\\\\\\' '); //像它恢复正常。 //注意在这些行中缺少`;`。我在链接`.replace()`函数。 输出: $ / pre $'基本上,您在寻找字界: \b 这里是更新的小提琴 I have some JavaScript code that transforms dumb quotes into smart quotes in a 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/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-dashesreturn a };Any ideas? Thanks!P.S. I suck at regular expressions… 解决方案 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: \bHere's an updated fiddle 这篇关于哑引号成聪明的引号Javascript问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 11:42