I try to split a string on a backslash. However, the split method doesn't return 2 elements but just 1 without the backslash:
'0.023750\0.023746'.split("\\")
> ["0.023750.023746"]
split method doesn't work, because "\0" is special symbol as "\n" or "\r" that means NULL. So that why your string is interpreted incorrectly. Solution is: write before your string symbol 'r'. It will say to interpreter to ignore special symbols.
r'0.023750\0.023746'.split("\\")
> ['0.023750', '0.023746']
Related
assert(JSON.stringify(searchForMatches(searchData,['cats']))==='["catcode.io","catgifs.co"]',"The result should be '[\"catcode.io\",\"catgifs.co\"]'");
Why do I need the use of \ at the end multiple times? It throws an error otherwise. Is it specific to JSON.stringify and/or when/how should I go about about using it in future instances?
\ is an escape operator. It prevents the language from parsing the next char as syntax relevant, so without it your string stops at the next " after the first " which starts the string.
The backslash is a JavaScript operator used within a string for escaping a special character.
In your case it is escaping a quotation. See this example:
// in this case it escapes the " symbol because that would end the string.
var x = "\"";
This is the same as:
// in this case you don't need to escape it because
// the string begins with the apostrophe instead of the " character
var x = '"';
In both cases console.log(x); will print a single quote character.
Here's my code:
let padded = "03";
ascii = `\u00${padded}`;
However, I receive Bad character escape sequence from Babel. I'm trying to end up with:
\u0003
in the ascii variable. What am I doing wrong?
EDIT:
Ended up with ascii = (eval('"\\u00' + padded + '"'));
What am I doing wrong?
A unicode escape sequence is basically atomic. You cannot really build one dynamically. Template literals basically perform string concatenation, so your code is equivalent to
'\00' + padded
It should be obvious now why you get that error. If you want to get the corresponding unicode character you can instead use String.fromCodePoint or String.fromCharCode:
String.fromCodePoint(3)
If you want a string that literally contains the character sequence \u0003, then you just need to escape the escape character to produce a literal backslash:
`\\u00${padded}`
I have seen all the questions asked previously but it didn't help me out . I have a string that contains backslash and i want to replace the backslashes with '-'
var s="adbc\sjhf\fkjfh\af";
s = s.replace(/\\/g,'-');
alert(s);
I thought this is the proper way to do it and of course i am wrong because in alert it shows adbcsjhffkjfhaf but i need it to be like adbc-sjhf-fkjfh-af.
What mistake i do here and what is the reason for it and how to achieve this...??
Working JS Fiddle
Your s is initially adbcsjhffkjfhaf. You meant
var s="adbc\\sjhf\\fkjfh\\af";
You need to double-up the backslashes in your input string:
var s="adbc\\sjhf\\fkjfh\\af";
Prefixing a character with '\' in a string literal gives special meaning to that character (eg '\t' means a tab character). If you want to actually include a '\' in your string you must escape it with a second backslash: '\\'
Javascript is ignoring the \ in \s \f \a in your string. Do a console.log(s) after assigning, you will understand.
You need to escape \ with \\. Like: "adbc\\sjhf\\fkjfh\\af"
The string doesn't contain a backslash, it contains the \a, \s and \f (escape sequence for Form Feed).
if you change your string to adbc\\sjhf\\fkjfh\\af
var s="adbc\\sjhf\\fkjfh\\af";
s = s.replace(/\\/g,'-');
alert(s);
you will be able to replace it with -
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.
Simple question... I am just baffled how to write the notation.
Example: input='..."aaaa\"bbbb"...'
I need regex to grab the string ignoring nested quotations.
I guess it can start like: input=input.replace(/[^\\]"...
How can I say 'all characters until a " which is not preceded by a \' ?
Thanks!
"([^"\\]|\\.)*"
Inside the quotes can be (a) any character aside from a quote or backslash, or (b) any character if it's escaped with a backslash. Repeat.