admin管理员组文章数量:1323225
I have a string: " \" "
. I would like to escape all unescaped double-quotes, i.e. add a backslash before "
if it's note there.
input = '" \\" "'
input.replace(???) == '\\" \\" \\"'
I've tried
input.replace(/(?!\\)"/g, '\\"')
It escapes second backslash twice ('\" \\" \"'
) for the reason I don't understand.
I've figured out
(' ' + input).replace(/([^\\])"/g, '$1\\"').slice(1)
But it looks ugly. It has to be a better way.
Update:
One more test case:
>> input = '" \\" \\\\" \\\\\\"'
-> '" \" \\" \\\"'
>> input.replace(???)
-> '\" \" \\\" \\\"'
None of my regular expressions can handle it.
I have a string: " \" "
. I would like to escape all unescaped double-quotes, i.e. add a backslash before "
if it's note there.
input = '" \\" "'
input.replace(???) == '\\" \\" \\"'
I've tried
input.replace(/(?!\\)"/g, '\\"')
It escapes second backslash twice ('\" \\" \"'
) for the reason I don't understand.
I've figured out
(' ' + input).replace(/([^\\])"/g, '$1\\"').slice(1)
But it looks ugly. It has to be a better way.
Update:
One more test case:
>> input = '" \\" \\\\" \\\\\\"'
-> '" \" \\" \\\"'
>> input.replace(???)
-> '\" \" \\\" \\\"'
None of my regular expressions can handle it.
Share Improve this question edited Sep 12, 2011 at 2:43 NVI asked Sep 11, 2011 at 23:51 NVINVI 15.1k17 gold badges68 silver badges104 bronze badges 3-
JS doesn't support lookbehinds, but I'm trying to emulate them.
[^\]
doesn't pile (throws SyntaxError). – NVI Commented Sep 12, 2011 at 0:15 -
Ah sorry, I guess, it's me who got confused then ;) Right,
\
is also the escape characters inside an expression. My apologies... deleted my ment as it does not make sense at all... – Felix Kling Commented Sep 12, 2011 at 0:16 -
Anyways, the backslash is escaped because although
\"
does not match,"
does. As your expression is not anchored, every position/character is tried to match. – Felix Kling Commented Sep 12, 2011 at 0:22
3 Answers
Reset to default 4What I have is scarcely better, but it does handle escaped backslashes too:
>>> var v= 'a\\"b'
>>> v
"a\"b"
>>> v.replace(/(\\*)(")/g, function(x) { var l = x.length; return (l % 2) ? x : x.substring(0,l-1) + '\\"' } )
"a\\"b"
>>> var v= 'a\\\\"b'
>>> v
"a\\"b"
>>> v.replace(/(\\*)(")/g, function(x) { var l = x.length; return (l % 2) ? x : x.substring(0,l-1) + '\\"' } )
"a\\"b"
If there are an odd number of slashes before a quote (1, 3, 5), the quote is already escaped; an even number (including zero), in needs escaping.
Made all the harder to read by the necessary of escaping the slashes in input and by the inability of the colorizer to understand the regexp expression...
Of course, you probably shouldn't even be doing this. If you have a raw string and you need something you can pass to (e.g.) eval
, consider $.toJSON
.
This worked for me:
var arf = input.replace(/(^|[^\\])"/g, '$1\\"');
It says, replace a quote, when preceded by beginning-of-string or anything-other-than-backslash, with backslash followed by quote.
String(str).replace(/[\\"']/g, "\\$&")
.replace(/[\r\n\u2028\u2029]/g,
function (x) {
switch (x) {
case '\n': return "\\n";
case '\r': return "\\r";
case '\u2028': return "\\u2028";
case '\u2029': return "\\u2029";
}
})
The String
call ensures that the input is a valid string.
The first replace will handle quotes and backslashes. The second handles embedded line terminators.
If you want to deal with an already quoted string, you can change the first replace to this:
'"' + String(str).replace(/^"|"$/g, "").replace(/[\\"]/g, "\\$&") + '"'
to unquote and requote.
本文标签: javascriptEscape quotes in a string with backslashStack Overflow
版权声明:本文标题:javascript - Escape quotes in a string with backslash - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742083483a2419835.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论