javascript Jquery-反斜杠字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4324734/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 11:02:16  来源:igfitidea点击:

Jquery- backslash character

javascriptreplaceescaping

提问by Samuel Beckett

I am having a problem trying to replace the backslash character from a string:

我在尝试替换字符串中的反斜杠字符时遇到问题:

var g = myReadString;
g = g.replace("\", "\\");

it is giving an error of unrecognized character.

它给出了无法识别字符的错误。

How could a simple \be replaced with four \\\\?

一个简单的怎么可以\用四个代替\\\\

I would appreciate any help, thanks. Pandy

我很感激任何帮助,谢谢。潘迪

回答by Gumbo

The \?is the begin of an escape sequence. If you mean to write \?literally, you need to write \\that is an escape sequence as well and will be interpreted as a single \?. So if you want to replace one \?by four \\\\, you need to write this:

\?是开始一个转义序列。如果你想按\?字面意思写,你也需要写成\\一个转义序列,并将被解释为单个\?. 所以如果你想\?用四替换一\\\\,你需要这样写:

g.replace("\", "\\\\")

But this will only replace the first occurrence of a single \?. To do a global replace you need to use a regular expression with the global match modifier:

但这只会替换第一次出现的单个\?. 要进行全局替换,您需要使用带有全局匹配修饰符的正则表达式:

g.replace(/\/g, "\\\\")

回答by Niet the Dark Absol

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

I think that's what you're looking for. Let me know if not.

我想这就是你要找的。如果没有,请告诉我。

回答by mikesir87

The backslash also serves as an escaping character. You can find a list of characters on this page... http://www.c-point.com/javascript_tutorial/special_characters.htm

反斜杠也用作转义字符。您可以在此页面上找到字符列表... http://www.c-point.com/javascript_tutorial/special_characters.htm

So, in order to search for, or replace a backslash, you have to escape the backslash. I actually just ran your code, and it doesn't work, as the backslash is escaping the first quote. What exactly are you trying to do? If you want to replace each single backslash with a double, you will need something like this.

因此,为了搜索或替换反斜杠,您必须转义反斜杠。我实际上只是运行了您的代码,但它不起作用,因为反斜杠正在转义第一个引号。你到底想做什么?如果你想用双倍替换每个反斜杠,你将需要这样的东西。

var g = myReadString;
g = g.replace("\", "\\");

Hope that helps!

希望有帮助!

回答by jensgram

In generalmake sure to always escape correctly.

一般确保始终正确逃生。

In your first argument for replace()you intend to pass a string containing \but it ends up as ",(quote-comma-space)! This is because you're actually escaping the "closing" quote on the string:

在您的第一个参数中,replace()您打算传递一个包含\但以",(quote-comma-space)结尾的字符串!这是因为您实际上是在转义字符串上的“结束”引号:

g = g.replace("\", "\\");
              ^    ^
              s    e
              t    n
              a    d
              r
              t

Now the first argument is the string quote-comma-space. The rest gives a syntax error!

现在第一个参数是字符串quote-comma-space。其余的给出了语法错误!

What you wanted:

你想要什么:

g = g.replace("\", "\\\\");
              ^  ^  ^        ^
              s  e  s        e
              t  n  t        n
              a  d  a        d
              r     r
              t     t

First argument: The string \
Second argument: The string \\\\

第一个参数:字符串\
第二个参数:字符串\\\\