替换 JavaScript 中的符号
有谁知道如何将下面的这 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您希望万无一失,那么知道引用的方向(左或右)并不容易。 如果始终保持正确并不那么重要,您可以使用几个正则表达式:
您会发现第二个 ' 没有正确更改。
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:
You'll see that the second ' doesn't change properly.
困难的部分是识别撇号,这会扰乱单引号的计数。
对于双引号,我认为找到所有双引号并将奇数替换为左弯引号,偶数替换为右弯引号是安全的。 除非你有嵌套引用的情况。
来源是什么? 英文文本? 代码?
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?
我最近编写了一个名为 jsPrettify 的排版美化引擎,它就是这样做的。 这是我使用的算法的仅引号版本(原始此处):
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):