Javascript 如何使用javascript在字符串中用escape-char双引号替换双引号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11516786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I replace a double-quote with an escape-char double-quote in a string using javascript?
提问by Samik Sengupta
Say I have a string variable (var str
) as follows-
假设我有一个字符串变量 ( var str
),如下所示-
Dude, he totally said that "You Rock!"
伙计,他完全是在说“你摇滚!”
Now If I'm to make it look like as follows-
现在,如果我要让它看起来如下-
Dude, he totally said that \"You Rock!\"
伙计,他完全说的是“你摇滚!”
How do I accomplish this using the JavaScript replace()
function?
如何使用 JavaScriptreplace()
函数完成此操作?
str.replace("\"","\\"");
is not working so well. It gives unterminated string literal
error.
str.replace("\"","\\"");
效果不佳。它给出了unterminated string literal
错误。
Now, if the above sentence were to be stored in a SQL database, say in MySQL as a LONGTEXT(or any other VARCHAR-ish) datatype, what else string optimizations I need to perform? Quotes and Commas are not very friendly with query strings. I'd appreciate a few suggestions on that matter as well.
现在,如果上面的句子要存储在 SQL 数据库中,比如在 MySQL 中作为LONGTEXT(或任何其他VARCHAR-ish)数据类型,我还需要执行哪些字符串优化?引号和逗号对查询字符串不是很友好。我也希望能就此提出一些建议。
回答by Willem D'Haeseleer
You need to use a global regular expression for this, try it this way
你需要为此使用全局正则表达式,试试这种方式
str.replace(/"/g, '\"');
check out regex syntax and options for the replace function here:
在此处查看替换函数的正则表达式语法和选项:
回答by nnnnnn
Try this:
尝试这个:
str.replace("\"","\\""); // (escape backslashes and embedded double-quotes)
Or, use single-quotes to quote your search and replace strings:
或者,使用单引号引用您的搜索并替换字符串:
str.replace('"','\"'); // (still need to escape the backslash)
UPDATE: As pointed out by helmus, if the first parameter passed to .replace()
is a string it will only replace the first occurrence. To replace globally you have to pass a regex with the g
(global) flag:
更新:正如 helmus 所指出的,如果传递给的第一个参数.replace()
是一个字符串,它只会替换第一次出现。要全局替换,您必须使用g
(全局)标志传递正则表达式:
str.replace(/"/g,"\\"");
// or
str.replace(/"/g,'\"');
But why are you even doing this in JavaScript? It's OK to use these escape characters if you have a string literal like:
但是为什么你甚至在 JavaScript 中这样做呢?如果您有这样的字符串文字,则可以使用这些转义字符:
var str = "Dude, he totally said that \"You Rock!\"";
But this is necessary onlyin a string literal. That is, if your JS variable is set to a value that a user typed in a form field you don't need to this escaping.
但这仅在字符串文字中是必需的。也就是说,如果您的 JS 变量设置为用户在表单字段中键入的值,则您不需要进行此转义。
Regarding your question about storing such a string in an SQL database, again you only need to escape the characters if you're embedding a string literal in your SQL statement - and remember that the escape characters that apply in SQL aren't (usually) the same as for JS. You'd do any SQL-related escaping server-side.
关于在 SQL 数据库中存储这样的字符串的问题,如果您在 SQL 语句中嵌入字符串文字,您只需要转义字符 - 并记住在 SQL 中应用的转义字符不是(通常)与 JS 相同。您可以在服务器端执行任何与 SQL 相关的转义操作。
回答by derekmc
The other answers will work for most strings, but you can end up unescaping an already escaped double quote, which is probably not what you want.
其他答案适用于大多数字符串,但您最终可能会取消转义已经转义的双引号,这可能不是您想要的。
To work correctly, you are going to need to escape all backslashes and then escape all double quotes, like this:
要正常工作,您需要转义所有反斜杠,然后转义所有双引号,如下所示:
var test_str = '"first \" middle \" last "';
var result = test_str.replace(/\/g, '\\').replace(/\"/g, '\"');
depending on how you need to use the string, and the other escaped charaters involved, this may still have some issues, but I think it will probably work in most cases.
根据您需要如何使用字符串以及涉及的其他转义字符,这可能仍然存在一些问题,但我认为在大多数情况下它可能会起作用。
回答by F0G
var str = 'Dude, he totally said that "You Rock!"';
var var1 = str.replace(/\"/g,"\\"");
alert(var1);