如何在字符串中使用backSlashes(\)?

发布于 2025-02-07 21:27:48 字数 397 浏览 2 评论 0原文

我尝试了多种方法来获取执行的单次拒绝 (我不是说> 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ぶ宁プ宁ぶ 2025-02-14 21:27:48

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

您已将问题标记为StringREGEX

在JavaScript中,Backslash在字符串文字和正则表达式中都具有特殊的意义。如果您想要在字符串或正则表达式中进行实际的后斜线,则必须编写两个:\\

以下字符串以为单位后斜线开始,您在字面上看到的第一个是逃脱字符启动逃脱序列\\逃脱序列告诉解析器在字符串中放置一个后斜切:

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

以下正则表达式将与 backslash(不是两个)匹配;同样,您在字面上看到的第一个是逃生角色启动逃生序列。 \\逃脱序列告诉解析器将单个后斜切字符放在正则表达式模式中:

var rex = /\\/;

如果您使用字符串来创建正则表达式(而不是像我一样使用正则表达式语言上面)请注意,您要处理两个级别:字符串级别和正则表达式级别。因此,要使用与单个后斜线匹配的字符串创建正则表达式,您最终使用 four

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

这是因为首先,您正在编写字符串文字,但是您想实际上将后斜切放在结果中。字符串,因此,您可以使用\\对所需的每个后挡板进行操作。但是,您的正则也需要对您想要的每个真实的后挡板都需要两个\\,因此需要在字符串中看到两个后斜线。因此,总共四个。这是我避免使用new Regexp(String)的原因之一。我很容易困惑。 :-)

ES2015和ES2018

快速更新至2015年, ad dolphin_wood of dolphin_wood指出 ,TAG函数和 函数:

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

str最终使字符\apple。只要小心您的模板文字中没有$ {,因为$ {在模板文字中启动替换。例如:

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

...最终是\ Applebar

(See ES2015 update at the end of the answer.)

You've tagged your question both string and regex.

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:

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

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:

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("\\\\");

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 using new 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:

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

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

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

...ends up being \applebar.

下壹個目標 2025-02-14 21:27:48

尝试string.raw方法:

str = String.raw`\apple` // "\apple"

参考: string.raw()

Try String.raw method:

str = String.raw`\apple` // "\apple"

Reference here: String.raw()

梦幻的味道 2025-02-14 21:27:48

\是一个逃生字符,然后是非特殊字符时,它不会成为文字\。相反,您必须将其加倍\\

console.log("\apple");  //-> "apple" 
console.log("\\apple"); //-> "\apple" 

没有办法获取原始的,原始的字符串定义或创建没有逃生字符的文字字符串。

\ is an escape character, when followed by a non-special character it doesn't become a literal \. Instead, you have to double it \\.

console.log("\apple");  //-> "apple" 
console.log("\\apple"); //-> "\apple" 

There is no way to get the original, raw string definition or create a literal string without escape characters.

世态炎凉 2025-02-14 21:27:48

请尝试以下一个对我有用的方法,而我将获得后拔输出

String sss="dfsdf\\dfds";
System.out.println(sss);

please try the below one it works for me and I'm getting the output with backslash

String sss="dfsdf\\dfds";
System.out.println(sss);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文