Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use backslashes (\) in a string?

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

like image 420
Mik Avatar asked Apr 06 '12 10:04

Mik


People also ask

Why is Python adding backslashes to string?

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return. Conversely, prefixing a special character with "\" turns it into an ordinary character.

How do you use a forward slash in a string in Python?

Use the str. split() method to split a string on the forward slashes, e.g. my_list = my_str. split('/') .

How do I add a backslash to a string in R?

Yes, you should. To write a single \ in a string, you write it as "\\" . This is because the \ is a special character, reserved to escape the character that follows it. (Perhaps you recognize \n as newline.)

What does backslash in string mean?

The backslash is used to escape special (unprintable) characters in string literals.


1 Answers

(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.

like image 131
T.J. Crowder Avatar answered Sep 28 '22 19:09

T.J. Crowder