why "\a" and "a" are the same thing in JavaScript? - 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

Related

Escape a character knowing only its index

I'm looking to escape a \n character knowing only its index. I am NOT looking to replace the character itself, but rather "prepend" an escape character to the existing \n, effectively escaping it.
For example, in JavaScript: '\\' + '\n' === '\\\n'
What I'm looking for is \\n not \\\n.
To reiterate, I do NOT want to replace \n entirely. I am well aware '\n'.replace('\n', '\\n') would do what I'm looking for. I simply want to prepend an escape character to the existing newline. Is there a reason that JS prepends a literal backslash and does not escape the newline?
'\\' + '\'' === "\\'"
I guess I'm wondering why the newlines behave differently. Thanks for any ideas!
It's not possible to prepend a backslash to a character in order to escape it. For example, with a newline character, '\n', to Javascript, this is a string containing one character code: 10.
for (const char of '\n') {
console.log(char.charCodeAt());
}
There is not actually an n (nor a literal backslash) anywhere in there - \n is simply the convention programmers use and understand to refer to a newline. If a string is composed of a literal backslash and a literal n, the character codes are completely different: there's a character code of 92 (for the backslash) and a character code of 110 (for the n).
for (const char of '\\n') {
console.log(char.charCodeAt());
}
Your only option is to completely replace the literal newline character with a backslash and an n (with '\n'.replace('\n', '\\n') - or, for a more general solution, construct a Map or object of literal characters and their escape sequences)

Regex for escape apostrophe(single quote) in word with single quotes [duplicate]

I am looking for a pattern that can find apostrophes that are inside single quotes. For example the text
Foo 'can't' bar 'don't'
I want to find and replace the apostrophe in can't and don't, but I don't want to find the single quotes
I have tried something like
(.*)'(.*)'(.*)'
and apply the replace on the second matching group. But for text that has 2 words with apostrophes this pattern won't work.
Edit: to clarify the text could have single quotes with no apostrophes inside them, which should be preserved as is. For example
'foo' 'can't' bar 'don't'
I am still looking for only apostrophes, so the single quotes around foo should not match
I believe you need to require "word" characters to appear before and after a ' symbol, and it can be done with a word boundary:
\b'\b
See the regex demo
To only match the quote inside letters use
(?<=\p{L})'(?=\p{L})
(?<=[[:alpha:]])'(?=[[:alpha:]])
(?U)(?<=\p{Alpha})'(?=\p{Alpha}) # Java, double the backslashes in the string literal
Or ASCII only
(?<=[a-zA-Z])'(?=[a-zA-Z])
You can use the following regular expression:
'[^']+'\s|'[^']+(')[^' ]+'
it will return 3 matches, and if capture group 1 participated in the word, it will be the apostrophe in the word:
'foo'
'can't'
'don't'
demo
How it works:
'[^']+'\s
' match an apostrophe
[^']+ followed by at least one character that isn't an apostrophe
' followed by an apostrophe
\s followed by a space
| or
'[^']+(')[^' ]+'
' match an apostrophe
[^']+ followed by at least one character that isn't an apostrophe
(') followed by an apostrophe, and capture it in capture group 1
[^' ]+ followed by at least one character that is not an apostrophe or a space
' followed by an apostrophe

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

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

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'

How does the regular expression `new RegExp("user=" + un + "[\"'\\s>]", "i")` work?

I am a Javascript Junkie and wishing to test around some codes.
new RegExp("user=" + un + "[\"'\\s>]", "i")
what will this actually mean?
It was from a site, and it actually works!
I especially don't get the [\"'\\s>] part.
[ and ] signify a character class. Basically, it will match one of any characters found within. The backslash \ escapes special characters. So you'll see that the first double-quote is escaped, showing we're looking for the " char within the value, and not merely using the " within our regular expression to wrap around a value. Then the single-quote ' is considered, followed by a backslash (literal - so it too must be escaped \\ looks for one backslash). It appears the original was indicating a space, which is \s. And then the greater-than symbol.
[\"'\s>] means any of the following characters: " ' space >
So, assuming un = "abc" your regex would match any of the following:
user=abc"
user=abc'
user=abc // There is a space after abc.
user=abc>
[\"'\\s>]
represents a char class containing 4 thing: a double quote, a single quote, a \s, and a >. It matches either one of these four once. The \s in turn could mean a single white space which could come from a space, tab, newline, carriage return.
The extra \ you see are for escaping.

Categories