寻找带引号的字符串生成器

发布于 2024-09-07 14:21:39 字数 209 浏览 3 评论 0原文

有谁知道有一个免费的网站或程序可以获取字符串并用适当的转义字符将其引用?

比方说,我想引用

It's often said that "devs don't know how to quote nested "quoted strings"".

并且我想指定是否用单引号或双引号括起来。我个人不关心除反斜杠之外的任何转义字符,但其他人可能会关心。

Does anyone know of a free website or program which will take a string and render it quoted with the appropriate escape characters?

Let's say, for instance, that I want to quote

It's often said that "devs don't know how to quote nested "quoted strings"".

And I would like to specify whether that gets enclosed in single or double quotes. I don't personally care for any escape character other than backslash, but other's might.

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

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

发布评论

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

评论(4

猫瑾少女 2024-09-14 14:21:39

如果字符串的双引号都没有被转义,您可以简单地执行以下操作:

str = str.replace(/"/g, "\\\"");

否则,您应该检查它是否已经转义,只有在没有转义时才进行替换;您可以为此使用lookbehind。以下是我首先想到的,但对于像 转义反斜杠后跟引号 \\" :( 这样的字符串会失败,

str = str.replace(/(?<!\\)"/g, "\\\"");

以下内容确保倒数第二个字符(如果存在)不是反斜杠 更新:

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

刚刚记住 JavaScript 不支持后视;您可以在支持后视的正则表达式引擎(如 perl/php/.net 等)上使用相同的正则表达式。

If none of the double quotes of the string is already escaped, you can simply do:

str = str.replace(/"/g, "\\\"");

Otherwise, you should check if it is already escaped and replace only if it isn't; You can use lookbehind for that. The following is what came to my mind first but it would fail for strings like escaped backslash followed by quotes \\" :(

str = str.replace(/(?<!\\)"/g, "\\\"");

The following makes sure that the second last character, if exists, is not a backslash.

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

Update: Just remembered that JavaScript doesn't support look-behind; you can use the same regex on a look-behind supporting regex engine like perl/php/.net etc.

临风闻羌笛 2024-09-14 14:21:39

任何像样的编程语言中的任何像样的正则表达式库都将具有执行此操作的功能 - 并不是说​​很难自己编写一个(正如其他答案所示)。因此,拥有一个单独的网站或程序来完成这项工作几乎是没有用的。

  • Perl 有 quotemeta 函数
  • PCRE 的 C++ 包装器有一个函数RE::QuoteMeta (警告:该链接处有巨型文件) PHP 具有相同的功能
  • preg_quote< /a> 如果您使用 Perl 兼容的正则表达式
  • Python 的 re 模块 有一个escape 函数
  • 在Java 中,java.util.regex.Pattern 类有一个quote 方法
  • Perl 和大多数基于 Perl 的其他正则表达式引擎都有元字符 \Q...\E,这意味着 \Q\E 之间的任何内容> 按字面解释
  • 大多数使用 POSIX 正则表达式的工具(例如 grep)都有一个选项,可以将输入解释为文字字符串(例如 grep -F

Any decent regex library in any decent programming language will have a function to do this - not that it's hard to write one yourself (as the other answers have indicated). So having a separate website or program to do it would be mostly useless.

  • Perl has the quotemeta function
  • PCRE's C++ wrapper has a function RE::QuoteMeta (warning: giant file at that link) which does the same thing
  • PHP has preg_quote if you're using Perl-compatible regexes
  • Python's re module has an escape function
  • In Java, the java.util.regex.Pattern class has a quote method
  • Perl and most of the other regular expression engines based on Perl have metacharacters \Q...\E, meaning that whatever comes between \Q and \E is interpreted literally
  • Most tools that use POSIX regular expressions (e.g. grep) have an option that makes them interpret their input as a literal string (e.g. grep -F)
断桥再见 2024-09-14 14:21:39

在 Python 中,用于用单引号引起来:

import re
mystr = """It's often said that "devs don't know how to quote nested "quoted strings""."""
print("""'%s'""" % re.sub("'", r"\'", mystr))

输出:

'It\'s often said that "devs don\'t know how to quote nested "quoted strings"".'

您可以轻松地将其改编为更通用的形式,和/或将其包装在脚本中以进行命令行调用。

In Python, for enclosing in single quotes:

import re
mystr = """It's often said that "devs don't know how to quote nested "quoted strings""."""
print("""'%s'""" % re.sub("'", r"\'", mystr))

Output:

'It\'s often said that "devs don\'t know how to quote nested "quoted strings"".'

You could easily adapt this into a more general form, and/or wrap it in a script for command-line invocation.

我早已燃尽 2024-09-14 14:21:39

所以,我想答案是“不”。抱歉,伙计们,我没有学到任何我不知道的东西。可能是我没有正确表述问题的错。

为所有发帖者+1

so, I guess the answer is "no". Sorry, guys, but I didn't learn anything that I don't know. Probably my fault for not phrasing the question correctly.

+1 for everyone who posted

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