why does '\\\[' equals '\\[' ? How does backslash work in string? - javascript

As the title
console.log('\\\[' === '\\[');
returns true.
Can anyone explain in detail what's the difference?

A backslash before most characters will only be parsed as an unnecessary escape character - the backslash will be ignored. This is what's happening in the second part of the first string. Before a certain few characters though, such as another backslash in \\, or \n, it will be parsed as a escape sequence. \\ is the escape sequence for a single literal backslash:
console.log('\\');
and is only one character.
A backslash before a [ will resolve to just the [, though:
console.log('\[');
So:
'\\\[' - A literal backslash, followed by an (unnecessarily escaped) [
'\\[' - A literal backslash, followed by a plain [
See MDN for a list of escape sequences.

In strings, the backslash (\) is a special character used to encode other special characters, including the backslash.
'\\[' is a JavaScript string literal that contains a backslash (\\) and an open square bracket ([). In the compiled program the string is \[.
'\\\[' is a JavaScript string literal that contains a correctly encoded backslash (\\) followed by the combination of characters \[ that looks like an escape sequence but doesn't mean anything. Because this combination is not defined and \ by itself does not mean anything, the JavaScript interpreter ignores the backslash and corrects the string; it becomes identical to the first one (\[).
The behaviour is documented:
For characters not listed in the table, a preceding backslash is ignored, but this usage is deprecated and should be avoided.

Backslash is a special character. Literally, JS talk to browser to interpret the symbol after \ as is. Sometimes it calls screening or shielding.
That is why we can write smth like that: console.log("Double \"quotes\" inside another one."); with the result of Double "quotes" inside another one. without any error. Although that is not the way we need to use anywhere.
"\\\[" separates into 2 parts: \\ and \[. First returns \ and the second returns [. Finally it is \[.
"\\[" separates into 2 parts: \\ and [. First returns \ and the second returns [. Finally it is \[.

Related

why "\a" and "a" are the same thing in JavaScript?

You know that "n" and "\n" are not same in JavaScript, cause the second one is a escape sequence, but why "\a" and "a" is the same? If you check charCodeAt of the two strings, you will know.
Can someone explain to me?
What exactly escape sequence is defined in JavaScript?
\a is not an special sequence (like \n or \t), so the \ falls back to being an escape character, meaning that the character following it will be used literally (even if it were a quote, or a special character).
Hence, '\a' === 'a'.
The second purpose of backslash (the first is printing special character like newline with \n or TAB with \t), is to escape JavaScript special character. For example, to have a string containing a quote, you can either mark the string with double quotes "'" or if you use single quotes, you will need to escape with the backslash, like so: '\'', to prevent the literal ' from terminating the string.
As you can see in this answer, not every letter has an associated escape sequence. 'a' is one of the letters that does NOT have an escape sequence associated with it, and so to Javascript, there's no special meaning, it's just a backslash and the letter 'a'.
only few letters in combination with a backslash form escape sequence (like \n, \f, \r, \b, \t, \v) and \a is not in the list.
please refer the following link
https://www.w3schools.com/js/js_strings.asp

Regex match with '\' slash and replace with '\\'?

I was converting normal string in to latex format.So i was created the latex code match and replace the \ single slash into \\ double slash.why the i need it Refer this link.I tried Below code :
function test(){
var tex="$$\left[ x=\left({{11}\over{2}}+{{\sqrt{3271}}\over{2\,3^{{{3}\over{2} $$";
var tex_form = tex.replace("/[\\\/\\\\\.\\\\]/g", "\\");
document.getElementById('demo').innerHTML=tex_form;//nothing get
}
test();
<p id="demo"></p>
Not getting any output data.But the match in this link
i wish to need replace the \ into \\
There are these issues:
The string literal has no backslashes;
The regular expression is not a regular expression;
The class in the intended regular expression cannot match sequences, only single characters;
The replacement would not add backslashes, only replace with them.
Here you find the details on each point:
1. How to Encode Backslashes in String Literals
Your tex variable has no backslashes. This is because a backslash in a string literal is not taken as a literal backslash, but as an escape for interpreting the character that follows it.
When you have "$$\left...", then the \l means "literal l", and so the content of your variable will be:
$$left...
As an l does not need to be escaped, the backslash is completely unnecessary, and these two assignments result in the same string value:
var tex="$$\left[ x=\left({{11}\over{2}}+{{\sqrt{3271}}\over{2\,3^{{{3}\over{2} $$";
var tex="$$left[ x=left({{11}over{2}}+{{sqrt{3271}}over{2,3^{{{3}over{2} $$";
To bring the point home, this will also represent the same value:
var tex="\$\$\l\e\f\t\[\ \x\=\l\e\f\t\(\{\{\1\1\}\o\v\e\r\{\2\}\}\+\{\{\s\q\r\t\{\3\2\7\1\}\}\o\v\e\r\{\2\,\3\^\{\{\{\3\}\o\v\e\r\{\2\}\ \$\$";
If you really want to have literal backslashes in your content (which I understand you do, as this is about LaTeX), then you need to escape each of those backslashes... with a backslash:
var tex="$$\\left[ x=\\left({{11}\\over{2}}+{{\\sqrt{3271}}\\over{2\\,3^{{{3}\\over{2} $$";
Now the content of your tex variable will be this string:
$$\left[ x=\left({{11}\over{2}}+{{\sqrt{3271}}\over{2\,3^{{{3}\over{2} $$
2. How to Code Regular Expression Literals
You are passing a string literal to the first argument of replace, while you really intend to pass a regular expression literal. You should leave out the quotes for that to happen. The / are the delimiters of a regular expression literal, not quotes:
/[\\\/\\\\\.\\\\]/g
This should not be wrapped in quotes. JavaScript understands the / delimiters as denoting a regular expression literal, including the optional modifiers at the end (like g here).
3. Classes are sets of single characters
This regular expression has unnecessary characters. The class [...] should list all individual characters you want to match. Currently you have these characters (after resolving the escapes):
\
/
\
\
.
\
\
It is overkill to have the backslash represented 5 times. Also, in JavaScript the forward slash and dot do not need to be escaped when occurring in a class. So the above regular expression is equivalent to this one:
/[\\/.]/g
Maybe this is, or is not, what you intended to match. To match several sequences of characters, you could use the | operator. This is just an example:
/\\\\|\\\/|\\\./g
... but I don't think you need this.
4. How to actually prefix with backslashes
It seems strange to me that you would want to replace a point or forward slash with a backslash. Probably you want to prefix those with a backslash. In that case make a capture group (with parentheses) and refer to it with $1 in this replace:
tex.replace(/([\\/.])/g, "\\$1");
Note again, that in the replacement string there is only one literal backslash, as the first one is an escape (see point 1 above).
why the i need it
As the question you link to says, the \ character has special meaning inside a JavaScript string literal. It represents an escape sequence.
Not getting any output data.But the match in this link
The escape sequence is processed when the string literal is parsed by the JavaScript compiler.
By the time you apply your regular expression to them, they have been consumed. The slash characters only exist in your source code, not in your data.
If you want to put a slash character in your string, then you need to write the escape sequence for it (the \\) in the source code. You can't add them back in with JavaScript afterwards.
Not sure if I understood the problem, but try this code:
var tex_form = tex.replace("/(\\)/g","\\\\");.
You need to use '(' ')' instead of '['']' to get a match for output.

JavaScript RegExp : Why causes a double backslash ( \\ ) an error? [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 2 years ago.
Have found this out by accident and have no idea what's the reason.
// Results in "Syntax error in regular Expression".
var re = RegExp('\\');
I know that the constructor-function expects a string as parameter. And that the backslash is used within strings to escape characters with special meaning. I know that I have to escape characters like \d to \\d .
So therefore: The right backslash should the interpreted as some normal character.
Instead it throws an error. Why?
Can anyone explain this to me?
\ is used to escape \ in strings, so to get \d as you wrote you need to do \\d.
Also in regexp you need to escape \ with \\.
So you have two escape syntaxes that need to take place in regexps, using a single \\ will mean \ in regexp which is not correct, because it needs to be escaped.
So to workaround this you need double escape: \\\\ - this will be a regex looking for \.
The string literal '\\' creates a string containing nothing but a single backslash character, because within string literals the backslash is an escape character.
A single backslash character is not a valid regular expression.
If you want a regex that matches a single backslash then that needs to be escaped within the regex, so you need to do either:
re = /\\/;
// or
re = new RegExp('\\\\');
I believe the reason you are getting this error is that the effective regex which you are feeding into the JavaScript engine is a single backslash \.
The reason for this is that the first backslash escapes the second one. So you are putting in a literal backslash, which doesn't make any sense.
The backslash \ is the escape character for regular expressions. Therefore a double backslash would indeed mean a single, literal backslash. \ (backslash) followed by any of [\^$. ?*+(){} escapes the special character to suppress its special meaning.

Javascript strings with symbol '\'

I am having difficulties handling symbol \ in javascript. Strings seems to ignore it, for example alert('a\b') would only alert a.
My goal is to write following function which would give me latex image:
function getLatexImage(tex)
{
return 'http://chart.apis.google.com/chart?cht=tx&chl=' + tex;
}
However calling getLatexImage('\frac{a}{b}') gives me: "http://chart.apis.google.com/chart?cht=tx&chl=rac{a}{b}"
\f is being ignored.
Any suggestions?
\ is an escape character. It starts an escape sequence.
\n is a new line. \t is a tab. An escape sequence that has no special meaning usually gets turned into the character on the RHS (so \b is b).
To have a backslash as data in a string literal you have to escape it:
alert('a\\b');
Use \\. Single slashes are used for special signs (like \n, \t..)
The backslash \ is a escape character in JavaScript and many other programming languages. If you want to output it, you'll need to escape it by itself.
\\a
for example would output \a.
That being said, if you want to use it in an url you should encode it for safety.
%5c
translates to \
You can simply escape it by adding another backslash
'\\b'

Regular Expression in JS: \\. does not match \n

I am getting a string containing newlines (/n), tabs (/t) and lowercase letters [a-z]. It is possible to do that by matching /\n|\t/. AFAIK the dot represents the wildcard.
Therefore I was wondering, why /\n|\t/ doesn't match the same things as /\\./
var text = 'test1 \ntest2';
text.split(/\n/) //['test1', 'test2']
text.split(/\./) //['test1 \ntest2']
text.split(/\\./) //['test1 \ntest2']
Shouldn't the \\. match the \n (newline)?
Let me try and answer all the points:
AFAIK the dot represents the wildcard.
No, in regex, we do not use the term "wildcard". It is a special regex (meta)character. A dot in JavaScript regex matches any character but a newline.
I was wondering, why /\n|\t/ doesn't match the same things as /\\./
Because /\n|\t/ matches 1 symbol, either a newline or tab, while the regex /\\./ matches a literal \ and a character other than a newline.
The \n and \t are escape sequences. That means that the \ is not a literal backaslash that, together with the following symbol forms a code unit, a string that cannot be written otherwise. Indeed, how can we write a line break on the paper with a pen? No way!
See more about JavaScript character escape sequences here.
Now,
text.split(/\n/) //['test1', 'test2']
True, your input string contains a line break, thus, you get two elements in the resulting array
text.split(/\./) //['test1 \ntest2']
No match was found because \. matches a literal dot. A dot that is escaped (that has a literal \ before it) in the regex stops being a special regex metacharacter, and just matches its literal representation. Your string has no dot, thus, no matches.
text.split(/\\./) //['test1 \ntest2']
Again, no match is found, as /\\./ looks for a literal \ followed by any character but a newline.
A hint: use your expressions at regex101.com, it will tell you what your regex can match on the right.
Here, with regex, you have a literal notation (/.../). In literal notation, \ is considered a literal, thus, you do not have to escape it twice. If you used a constructor notation (i.e. RegExp(....)), you would have to use double escaping. E.g.
var re = /\\./; // is equal to
var re = new RegExp("\\\\.");
See more about constructor and literal notations at MDN RegExp help page.
\n gets evaluated to a new line, so you're essentially matching against an empty string. If you do a quick console.log('\n'); you can see the output of that.

Categories