如何在字符串中使用反斜杠(\)? [英] How can I use backslashes (\) in a string?

查看:466
本文介绍了如何在字符串中使用反斜杠(\)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了多种方法从执行获取 单反斜杠 (我不是指来自 HTML )。

I tried many ways to get a single backslash from an executed (I don't mean an input from html).

我可以将特殊字符作为标签,新行和许多其他人将其转义为 \\t \\ n \\(其他角色)但我当非特殊字符位于其旁边时,无法获得单个反斜杠。

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.

我不想要这样的东西:

str = "\apple";   // I want this, to return:
console.log(str); // \apple

如果我尝试将字符设为0,那么我得到 a 而不是 \

and if I try to get character at 0 then I get a instead of \.

推荐答案

(请参阅答案末尾的ES2015更新。)

您已将问题标记为 string regex

在JavaScript中,反斜杠在字符串中都有特殊含义文字和正则表达式。如果你想在字符串或正则表达式中使用实际的反斜杠,你必须写两个: \\

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: \\.

这个字符串以一个反斜杠开头,你在文字中看到的第一个是一个转义字符,告诉我们按字面意思取下一个字符:

This string starts with one backslash, the first one you see in the literal is an escape character telling us to take the next character literally:

var str = "\\I have one backslash";

此正则表达式将匹配单个反斜杠(不是两个);再一次,你在文字中看到的第一个是一个转义字符,告诉我们按字面意思取下一个字符:

This regular expression will match a single backslash (not two); again, the first one you see in the literal is an escape character telling us to take the next character literally:

var rex = /\\/;

如果你使用字符串来创建正则表达式(而不是使用正则表达式文字作为我在上面做过),请注意你正在处理两个级别:字符串级别和正则表达式级别。因此,要使用与单个反斜杠匹配的字符串创建正则表达式,最后使用

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:

// Matches *one* backslash
var rex = new RegExp("\\\\");

这是因为首先,你正在写一个字符串文字,但你想在其中加入反斜杠。所以你用 \\ 为你想要的每一个反斜杠做到这一点。但是你的正则表达式需要两个 \\ 来获得你想要的每一个真正的反斜杠,所以它需要在字符串中看到两个反斜杠。因此,共有四个。这是我尽可能避免使用新的RegExp(字符串)的原因之一;我很容易混淆。 : - )

That's because first, you're writing a string literal, but you want to actually put backslashes in it. 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 using new RegExp(string) whenver I can; I get confused easily. :-)

快进到2015年,正如Dolphin_Wood指出的那样新的ES2015标准为我们提供了模板文字,标签功能和 String.raw 功能:

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:

// Yes, this unlikely-looking syntax is actually valid ES2015
let str = String.raw`\apple`;

str 最终得到字符 \ a p p l ,以及 e 。请注意,字符串(模板)中没有 $ {,因为这是模板文字和<$​​ c $ c> $ {开始替换。例如:

str ends up having the characters \, a, p, p, l, and e in it. Just be careful there are no ${ in your "string" (template), since this is a template literal and ${ starts a substitution. E.g.:

let foo = "bar";
let str = String.raw`\apple${foo}`;

...最终为 \ applebar 。另请注意,这与C#中的逐字字符串不太相似,因为与 LegacyOctalEscapeSequence 并导致语法错误。所以例如

...ends up being \applebar. Also note this isn't quite like "verbatim" strings in C# or similar, as sequences matching the LegacyOctalEscapeSequence in the spec are not allowed and cause a syntax error. So for instance

// Fails
let str = String.raw`c:\foo\12\bar`;

...因为 \12 而失败看起来像遗留的八进制文字。

...fails because \12 looks like a legacy octal literal.

这篇关于如何在字符串中使用反斜杠(\)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆