转换“直引号”到“弯引号”

发布于 2024-08-20 15:50:32 字数 219 浏览 3 评论 0原文

我有一个使用基于 Javascript 的规则引擎的应用程序。我需要一种将常规直引号转换为弯引号(或智能)引号的方法。为 ["] 执行 string.replace 会很容易,但这只会插入一种大写引号。

我能想到的最好方法是用左弯引号替换第一次出现的引号,然后用左弯引号替换其余的引号,

有没有办法使用 Javascript 来完成此操作?

I have an application which uses a Javascript-based rules engine. I need a way to convert regular straight quotes into curly (or smart) quotes. It’d be easy to just do a string.replace for ["], only this will only insert one case of the curly quote.

The best way I could think of was to replace the first occurrence of a quote with a left curly quote and every other one following with a left, and the rest right curly.

Is there a way to accomplish this using Javascript?

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

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

发布评论

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

评论(7

坐在坟头思考人生 2024-08-27 15:50:32

您可以将单词字符前面的所有内容替换为左引号,将单词字符后面的所有内容替换为右引号。

str = str.replace(/"(?=\w|$)/g, "“");
str = str.replace(/(?<=\w|^)"/g, "”"); // IF the language supports look-
                                             // behind. Otherwise, see below.

正如下面的评论所指出的,这不会考虑标点符号,但很容易:

/(?<=[\w,.?!\)]|^)"/g

[编辑:] 对于不支持后视的语言,例如 Javascript,只要您首先替换所有前面的,您有两个选择:

str = str.replace(/"/g, "”"); // Replace the rest with right curly quotes
// or...
str = str.replace(/\b"/g, "”"); // Replace any quotes after a word
                                      // boundary with right curly quotes

(我保留了上面的原始解决方案,以防这对使用支持后视的语言的人有帮助)

You could replace all that preceed a word character with the left quote, and all that follow a word character with a right quote.

str = str.replace(/"(?=\w|$)/g, "“");
str = str.replace(/(?<=\w|^)"/g, "”"); // IF the language supports look-
                                             // behind. Otherwise, see below.

As pointed out in the comments below, this doesn't take punctuation into account, but easily can:

/(?<=[\w,.?!\)]|^)"/g

[Edit:] For languages that don't support look-behind, like Javascript, as long as you replace all the front-facing ones first, you have two options:

str = str.replace(/"/g, "”"); // Replace the rest with right curly quotes
// or...
str = str.replace(/\b"/g, "”"); // Replace any quotes after a word
                                      // boundary with right curly quotes

(I've left the original solution above in case this is helpful to someone using a language that does support look-behind)

好听的两个字的网名 2024-08-27 15:50:32

您可能想看看 Pandoc 做了什么——显然是使用 --smart选项,它在所有情况下都能正确处理引号(包括例如 'tis 和 'twere)。

我最近编写了一个 Javascript 排版美化引擎,除其他外,它还可以替换引号;我基本上使用了 Renesis 建议的算法,但是目前有一个失败的测试正在等待更智能的解决方案。

如果您有兴趣抄袭我的代码(和/或根据您所做的工作提交补丁),请查看:jsPrettifyjsprettify.prettifyStr 可以满足您的需求。如果您不想处理 Closure 依赖项,可以使用 旧版本可以独立运行——甚至可以在Rhino中运行。

You might want to look at what Pandoc does—apparently with the --smart option, it handles quotes properly in all cases (including e.g. ’tis and ’twere).

I recently wrote a Javascript typography prettification engine that does, among other things, quote replacement; I wound up using basically the algorithm suggested by Renesis, but there’s currently a failing test up waiting for a smarter solution.

If you’re interested in cribbing my code (and/or submitting a patch based on work you’ve done), check it out: jsPrettify. jsprettify.prettifyStr does what you’re looking for. If you don’t want to deal with the Closure dependency, there’s an older version that runs on its own—it even works in Rhino.

许你一世情深 2024-08-27 15:50:32
'foo "foo bar" "bar"'.replace(/"([-a-zA-Z0-9 ]+)"/g, function(wholeMatch, m1){
    return "“" + m1 + "”";
});
'foo "foo bar" "bar"'.replace(/"([-a-zA-Z0-9 ]+)"/g, function(wholeMatch, m1){
    return "“" + m1 + "”";
});
凉城凉梦凉人心 2024-08-27 15:50:32

以下只是通过交替更改每个引号(但是这个特定示例将省略孤立的引号)。

str.replace(/\"([^\"]*)\"/gi,"“$1”");

只要您正在纹理化的文本没有因双引号的不当使用而搞砸,就可以完美地工作。在英语中,引号永远不会嵌套。

The following just changes every quote by alternating (this specific example however would leave out the orphaned quotes).

str.replace(/\"([^\"]*)\"/gi,"“$1”");

Works perfectly, as long as the text you're texturizing isn't already screwed up with improper use of the double quote. In English, quotes are never nested.

情仇皆在手 2024-08-27 15:50:32

我认为这样的事情一般来说一点也不容易,因为您必须准确解释内容中每个双引号字符的含义。也就是说,我要做的就是收集我感兴趣的所有文本节点,然后遍历并跟踪每个双引号实例的“开/关”(或“奇/偶”;无论如何)性质。然后您就可以知道要使用哪个替换实体。

I don't think something like that in general is easy at all, because you'd have to interpret exactly what each double-quote character in your content means. That said, what I'd do is collect all the text nodes I was interested in, and then go through and keep track of the "on/off" (or "odd/even"; whatever) nature of each double quote instance. Then you can know which replacement entity to use.

四叶草在未来唯美盛开 2024-08-27 15:50:32

我在这里没有找到我想要的逻辑,所以这就是我最终的结果。

value = value.replace(/(^|\s)(")/g, "$1“"); // replace quotes that start a line or follow spaces
value = value.replace(/"/g, "”"); // replace rest of quotes with the back smart quote

我有一个小文本区域,需要将直引号替换为弯引号(智能)。我只是在 keyup 上执行这个逻辑。我试图让它表现得像 Microsoft Word。

I didn't find the logic I wanted here, so here's what I ended up going with.

value = value.replace(/(^|\s)(")/g, "$1“"); // replace quotes that start a line or follow spaces
value = value.replace(/"/g, "”"); // replace rest of quotes with the back smart quote

I have a small textarea that I need to replace straight quotes with curly (smart) quotes. I'm just executing this logic on keyup. I tried to make it behave like Microsoft Word.

去了角落 2024-08-27 15:50:32

为后人发帖。

根据@Steven Dee的建议,我去了Pandoc

我尽可能使用成熟且经过测试的工具,而不是烘焙自己的正则表达式。手工构建的正则表达式可能过于贪婪,或者不够贪婪,并且它们可能对单词边界和逗号等不敏感。Pandoc 占了大部分。

从命令行( --smart 参数打开智能引号):

pandoc --smart --standalone -o output.html input.html

..我知道命令行脚本可能会或可能不符合OP使用Javascript的要求。 (相关:如何在 Javascript 中执行 shell 命令

Posting for posterity.

As suggested by @Steven Dee, I went to Pandoc.

I try to use a mature and tested tool whenever I can versus baking my own regex. Hand built regex's can be overly greedy, or not greedy enough, and they may not be sensitive to word boundaries and commas etc. Pandoc accounts for most this and more.

From the command line (the --smart parameter turns on smart quotes):

pandoc --smart --standalone -o output.html input.html

..and I know a command line script may or may not fit OP's requirement of using Javascript. (related: How to execute shell command in Javascript)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文