why backslash(\) in a string is giving error in console - javascript

i have a string like
"C:\projects\cisco\iwan_staging_enc\enterprise-network-controller\ui-plugins\iwan"
when i paste into console and press enter, it is giving following error as
Uncaught SyntaxError: Invalid Unicode escape sequence
whats wrong here
Thanks
nageshwar

Since backslash is an escape character your string should be modified to:
"C:\\projects\\cisco\\iwan_staging_enc\\enterprise-network-controller\\ui-plugins\\iwan"
Please see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#Escape_notation

The \u is the start of a unicode escape sequence, in your string you have a \u not followed by four hex numbers which is the format of unicode escape sequence \uxxxx. See
"C:\projects\cisco\iwan_staging_enc\enterprise-network-controller\u0050i-plugins\iwan"
\u0050 id P
Also there there are other types of escapes, so for instance if you had a \n somewhere in there you would get a newline
"C:\new projects\cisco\iwan_staging_enc\enterprise-network-controller\u0050i-plugins\iwan"
So if you do not want avoid these escape sequences escape the \s in the string with a slash before it.
"C:\\projects\\cisco\\iwan_staging_enc\\enterprise-network-controller\\ui-plugins\\iwan"

Related

Length property not returning the desired value

I tried to get the length of this string:
$args[0]-match'\d.*?\/(.*)';$matches[1]
using:
console.log("$args[0]-match'\d.*?\/(.*)';$matches[1]".length);
I did this in the browser console. It returns 37. However, counting by hand, this string is 39 characters long. Am I missing something or is it a bug in the browser?
backslash character \ is a special escape character in strings so it doesn't count.
you can make backslashes count by preceding them with another backslash (that is escape the escape character):
console.log("$args[0]-match'\\d.*?\\/(.*)';$matches[1]".length)
\d is a char, no two.
you have to escape it
"$args[0]-match'\\d.*?\\/(.*)';$matches[1]".length

Regular expression to parse escape characters

I have a text field which accepts only one character but if user enters any escape characters such as \n, \t, \s, etc it should be allowing it even though it has 2 characters.
I am using jquery.validationEngine to validate the text field but failing to parse it as per my desire.
A regular expression for that could be
[^\\]|\\[tns]
Meaning
any char except a backslash
or a backslash followed by t, n or s
Note that if you need to put this in a string literal then backslashes must be doubled again (doubled once for the string and doubled again to escape their special meaning in a regexp).

Getting an error with Console.log('\x') in JavaScript

the problem occurs only when the word after the '\' begin with 'x'. I would like to know if \x is reserved word.
It is not a reserved word, it's a way to print a character by its code:
>>> console.log('\x56');
V
In fact, Firebug tells you what's wrong if you omit the number:
SyntaxError: malformed hexadecimal character escape sequence
Like in many C-style languages, backslashes denote "escape sequences". More can be found here.
Backslash is used to escape characters. If you need a literal backslash, use \\.

Bug with Javascript's JSON.parse?

console.log(JSON.parse('{"data":"{\"json\":\"rocks\"}"}'));
gives error (tested on Firefox and Chrome's console). Is this a bug with JSON.parse? Same decodes well when tested with PHP.
print_r(json_decode('{"data":"{\"json\":\"rocks\"}"}', true));
This string is processed differently in PHP and JS, i.e. you get different results.
The only escapes sequences in single quoted strings in PHP are \\ and \'. All others are outputted literally, according to the documentation:
To specify a literal single quote, escape it with a backslash (\). To specify a literal backslash, double it (\\). All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning.
In JS on the other hand, if a string contains an invalid escape sequence, the backslash is discarded (CV means character value):
The CV of CharacterEscapeSequence :: NonEscapeCharacter is the CV of the NonEscapeCharacter.
The CV of NonEscapeCharacter :: SourceCharacter but not EscapeCharacter or LineTerminator is the SourceCharacter character itself.
The quote might not be helpful by itself, but if you follow the link and have a look at the grammar, it should become clear.
So in PHP the string will literally contain \" while in JS it will only contains ", which makes it invalid JSON:
{"data":"{"json":"rocks"}"}
If you want to create a literal backslash in JS, you have to escape it:
'{"data":"{\\"json\\":\\"rocks\\"}"}'
To have a literal backslash in a string literal,you need \\.
console.log(JSON.parse('{"data":"{\\"json\\":\\"rocks\\"}"}'));
This will successfully escape the inner quotation marks for the JSON processing.
You need to escape the backslashes:
console.log(JSON.parse('{"data":"{\\"json\\":\\"rocks\\"}"}'));​
object with one or more then '\' wont return Object by JSON.parser, It will return the string again with skipping one '\'.
You can do parse again and again until all '\' skipped.
myobj = {\"json\":\"rocks\"}
myobj = {\\"json\\":\\"rocks\\"}
Following lines worked for me
remove backslash
while(typeof myobj == 'string'){
myobj = JSON.parse(myobj)
}
You don't really need to escape double quotes inside single quotes and you have two extra quotes in your input around inner object, just
console.log(JSON.parse('{"data":{"json":"rocks"}}'));
is enough.

Javascript CR+LF will break string?

When storing '\n\r' inside a string constant it will make the Javascript engine throw an error like "unterminated string" and so on.
How to solve this?
More info: basically I want to use Javascript to select text into a TEXTAREA HTML field and insert newlines. When trying to stuff those constants, I get an error.
String literals must not contain plain line break characters like CR and LF:
A 'LineTerminator' character cannot appear in a string literal, even if preceded by a backslash \. The correct way to cause a line terminator character to be part of the string value of a string literal is to use an escape sequence such as \n or \u000A.
So having a line break like this is invalid:
"foo
bar"
Instead you need to use an escape sequence like:
"foo\nbar"

Categories

Resources