替换 JavaScript 中的符号

发布于 2024-07-23 01:45:20 字数 123 浏览 7 评论 0原文

有谁知道如何将下面的这 2 个符号从字符串替换为代码?

  • '左单引号变为'
  • '右单引号变为'
  • "左双引号变为“
  • ”右双引号变为”

Does anyone know how can I replace this 2 symbol below from the string into code?

  • ' left single quotation mark into ‘
  • ' right single quotation mark into ’
  • " left double quotation mark into “
  • " right double quotation mark into ”

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绿萝 2024-07-30 01:45:20

如果您希望万无一失,那么知道引用的方向(左或右)并不容易。 如果始终保持正确并不那么重要,您可以使用几个正则表达式:

function curlyQuotes(inp) {
    return inp.replace(/(\b)'/, "$1’")
              .replace(/'(\b)/, "‘$1")
              .replace(/(\b)"/, "$1”")
              .replace(/"(\b)/, "“$1")
}

curlyQuotes("'He said that he was \"busy\"', said O'reilly")
// ‘He said that he was “busy”', said O’reilly

您会发现第二个 ' 没有正确更改。

Knowing which way to make the quotes go (left or right) won't be easy if you want it to be foolproof. If it's not that important to make it exactly right all the time, you could use a couple of regexes:

function curlyQuotes(inp) {
    return inp.replace(/(\b)'/, "$1’")
              .replace(/'(\b)/, "‘$1")
              .replace(/(\b)"/, "$1”")
              .replace(/"(\b)/, "“$1")
}

curlyQuotes("'He said that he was \"busy\"', said O'reilly")
// ‘He said that he was “busy”', said O’reilly

You'll see that the second ' doesn't change properly.

小镇女孩 2024-07-30 01:45:20

困难的部分是识别撇号,这会扰乱单引号的计数。

对于双引号,我认为找到所有双引号并将奇数替换为左弯引号,偶数替换为右弯引号是安全的。 除非你有嵌套引用的情况。

来源是什么? 英文文本? 代码?

The hard part is identifying apostrophes, which will mess up the count of single-quotes.

For the double-quotes, I think it's safe to find them all and replace the odd ones with the left curly and the even ones with the right curly. Unless you have a case with nested quotations.

What is the source? English text? Code?

烙印 2024-07-30 01:45:20

我最近编写了一个名为 jsPrettify 的排版美化引擎,它就是这样做的。 这是我使用的算法的仅引号版本(原始此处):

prettifyStr = function(text) {
  var e = {
    lsquo:  '\u2018',
    rsquo:  '\u2019',
    ldquo:  '\u201c',
    rdquo:  '\u201d',
  };
  var subs = [
    {pattern: "(^|[\\s\"])'",      replace: '$1' + e.lsquo},
    {pattern: '(^|[\\s-])"',       replace: '$1' + e.ldquo},
    {pattern: "'($|[\\s\"])?",     replace: e.rsquo + '$1'},
    {pattern: '"($|[\\s.,;:?!])',  replace: e.rdquo + '$1'}
  ];
  for (var i = 0; i < subs.length; i++) {
    var sub = subs[i];
    var pattern = new RegExp(sub.pattern, 'g');
    text = text.replace(pattern, sub.replace);
  };
  return text;
};

I recently wrote a typography prettification engine called jsPrettify that does just that. Here's a quotes-only version the algorithm I use (original here):

prettifyStr = function(text) {
  var e = {
    lsquo:  '\u2018',
    rsquo:  '\u2019',
    ldquo:  '\u201c',
    rdquo:  '\u201d',
  };
  var subs = [
    {pattern: "(^|[\\s\"])'",      replace: '$1' + e.lsquo},
    {pattern: '(^|[\\s-])"',       replace: '$1' + e.ldquo},
    {pattern: "'($|[\\s\"])?",     replace: e.rsquo + '$1'},
    {pattern: '"($|[\\s.,;:?!])',  replace: e.rdquo + '$1'}
  ];
  for (var i = 0; i < subs.length; i++) {
    var sub = subs[i];
    var pattern = new RegExp(sub.pattern, 'g');
    text = text.replace(pattern, sub.replace);
  };
  return text;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文