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

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

问题描述

我尝试了很多方法来从 执行 中获取单个反斜杠(我不是指来自 html).

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

我可以获取特殊字符作为制表符、换行符和许多其他字符,然后将它们转义为 \t\n\(someother character) 但是当一个非特殊字符在它旁边时我无法得到一个反斜杠.

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 更新.)

您已将问题标记为 stringregex.

You've tagged your question both string and 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("\\");

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

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>、le 在其中.请注意您的字符串"中没有 ${(template),因为这是一个模板文字并且 ${ 开始一个替换.例如:

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.更新:以下内容不再适用于 ES2018,它更新了模板文字以允许无效的转义序列,详细信息 此处.另请注意,这不太像逐字逐句"C# 或类似的字符串,作为与 <匹配的序列em>规范中的 LegacyOctalEscapeSequence 是不允许的,会导致语法错误.所以例如

...ends up being applebar. Update: The following is no longer true as of ES2018, which updated template literals to allow invalid escape sequences, details here. 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:foo12ar`;

...失败,因为 12 看起来像一个遗留的八进制文字.

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

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

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