Ale*_*lex 0 html javascript quotes replace
我有一些JavaScript代码可以将愚蠢的引号转换为智能引号contenteditable.
当您在它们仅关闭的行的开头添加哑引号时,会出现此问题.例如,你得到这个:
”dumb quotes” instead of “dumb quotes”
Run Code Online (Sandbox Code Playgroud)
试试演示: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 };
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?谢谢!
PS我很喜欢正则表达式......
试试这个:
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.
Run Code Online (Sandbox Code Playgroud)
输出:
'“dumb quotes” instead — of “dumb quotes”, fixed it's'
Run Code Online (Sandbox Code Playgroud)
基本上,你正在寻找单词边界:\b