寻找带引号的字符串生成器
有谁知道有一个免费的网站或程序可以获取字符串并用适当的转义字符将其引用?
比方说,我想引用
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果字符串的双引号都没有被转义,您可以简单地执行以下操作:
否则,您应该检查它是否已经转义,只有在没有转义时才进行替换;您可以为此使用lookbehind。以下是我首先想到的,但对于像
转义反斜杠后跟引号 \\" :(
这样的字符串会失败,以下内容确保倒数第二个字符(如果存在)不是反斜杠 更新:
刚刚记住 JavaScript 不支持后视;您可以在支持后视的正则表达式引擎(如 perl/php/.net 等)上使用相同的正则表达式。
If none of the double quotes of the string is already escaped, you can simply do:
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 \\" :(
The following makes sure that the second last character, if exists, is not a backslash.
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.
任何像样的编程语言中的任何像样的正则表达式库都将具有执行此操作的功能 - 并不是说很难自己编写一个(正如其他答案所示)。因此,拥有一个单独的网站或程序来完成这项工作几乎是没有用的。
quotemeta
函数RE::QuoteMeta
(警告:该链接处有巨型文件) PHP 具有相同的功能preg_quote
< /a> 如果您使用 Perl 兼容的正则表达式re
模块 有一个escape
函数java.util.regex.Pattern
类有一个quote
方法\Q...\E
,这意味着\Q
和\E
之间的任何内容> 按字面解释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.
quotemeta
functionRE::QuoteMeta
(warning: giant file at that link) which does the same thingpreg_quote
if you're using Perl-compatible regexesre
module has anescape
functionjava.util.regex.Pattern
class has aquote
method\Q...\E
, meaning that whatever comes between\Q
and\E
is interpreted literallygrep
) have an option that makes them interpret their input as a literal string (e.g.grep -F
)在 Python 中,用于用单引号引起来:
输出:
您可以轻松地将其改编为更通用的形式,和/或将其包装在脚本中以进行命令行调用。
In Python, for enclosing in single quotes:
Output:
You could easily adapt this into a more general form, and/or wrap it in a script for command-line invocation.
所以,我想答案是“不”。抱歉,伙计们,我没有学到任何我不知道的东西。可能是我没有正确表述问题的错。
为所有发帖者+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