如何在字符串中使用backSlashes(\)?
我尝试了多种方法来获取> html
的输入)。
我可以将特殊字符作为选项卡,新行和许多其他行,然后将它们逃脱到\ t
或\ n
或\(某些其他人)字符)
,但是当a 非特殊字符旁边时,我无法获得单个后斜线。
我不想要类似的东西:
str = "\apple"; // I want this, to return:
console.log(str); // \apple
如果我尝试以0获取字符,那么我得到a
而不是\
。
I tried many ways to get a single backslash from an executed (I don't mean an input from html
).
I can get special characters as tab, new line and many others then escape them to \t
or \n
or \(someother character)
but I cannot get a single backslash when a non-special character is next to it.
I don't want something like:
str = "\apple"; // I want this, to return:
console.log(str); // \apple
and if I try to get character at 0 then I get a
instead of \
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
(请参阅答案末尾的ES2015更新。)
您已将问题标记为
String
和REGEX
。在JavaScript中,Backslash在字符串文字和正则表达式中都具有特殊的意义。如果您想要在字符串或正则表达式中进行实际的后斜线,则必须编写两个:
\\
。以下字符串以为单位后斜线开始,您在字面上看到的第一个是逃脱字符启动逃脱序列。
\\
逃脱序列告诉解析器在字符串中放置一个后斜切:以下正则表达式将与单 backslash(不是两个)匹配;同样,您在字面上看到的第一个是逃生角色启动逃生序列。
\\
逃脱序列告诉解析器将单个后斜切字符放在正则表达式模式中:如果您使用字符串来创建正则表达式(而不是像我一样使用正则表达式语言上面)请注意,您要处理两个级别:字符串级别和正则表达式级别。因此,要使用与单个后斜线匹配的字符串创建正则表达式,您最终使用 four :
这是因为首先,您正在编写字符串文字,但是您想实际上将后斜切放在结果中。字符串,因此,您可以使用
\\
对所需的每个后挡板进行操作。但是,您的正则也需要对您想要的每个真实的后挡板都需要两个\\
,因此需要在字符串中看到两个后斜线。因此,总共四个。这是我避免使用new Regexp(String)
的原因之一。我很容易困惑。 :-)ES2015和ES2018
快速更新至2015年, ad dolphin_wood of dolphin_wood指出 ,TAG函数和 函数:
str
最终使字符\
,a
,p
,p
,l
和e
。只要小心您的模板文字中没有$ {
,因为$ {
在模板文字中启动替换。例如:...最终是
\ Applebar
。(See ES2015 update at the end of the answer.)
You've tagged your question both
string
andregex
.In JavaScript, the backslash has special meaning both in string literals and in regular expressions. If you want an actual backslash in the string or regex, you have to write two:
\\
.The following string starts with one backslash, the first one you see in the literal is an escape character starting an escape sequence. The
\\
escape sequence tells the parser to put a single backslash in the string:The following regular expression will match a single backslash (not two); again, the first one you see in the literal is an escape character starting an escape sequence. The
\\
escape sequence tells the parser to put a single backslash character in the regular expression pattern:If you're using a string to create a regular expression (rather than using a regular expression literal as I did above), note that you're dealing with two levels: The string level, and the regular expression level. So to create a regular expression using a string that matches a single backslash, you end up using four:
That's because first, you're writing a string literal, but you want to actually put backslashes in the resulting string, so you do that with
\\
for each one backslash you want. But your regex also requires two\\
for every one real backslash you want, and so it needs to see two backslashes in the string. Hence, a total of four. This is one of the reasons I avoid usingnew RegExp(string)
whenver I can; I get confused easily. :-)ES2015 and ES2018 update
Fast-forward to 2015, and as Dolphin_Wood points out the new ES2015 standard gives us template literals, tag functions, and the
String.raw
function:str
ends up having the characters\
,a
,p
,p
,l
, ande
in it. Just be careful there are no${
in your template literal, since${
starts a substitution in a template literal. E.g.:...ends up being
\applebar
.尝试
string.raw
方法:参考: string.raw()
Try
String.raw
method:Reference here: String.raw()
\
是一个逃生字符,然后是非特殊字符时,它不会成为文字\
。相反,您必须将其加倍\\
。没有办法获取原始的,原始的字符串定义或创建没有逃生字符的文字字符串。
\
is an escape character, when followed by a non-special character it doesn't become a literal\
. Instead, you have to double it\\
.There is no way to get the original, raw string definition or create a literal string without escape characters.
请尝试以下一个对我有用的方法,而我将获得后拔输出
please try the below one it works for me and I'm getting the output with backslash