Confusion regarding regex - javascript

I just found this regex in JavaScript
var str=input.replace(/\0/g, "\\0");
Can you please explain me what does it mean? What is the meaning of /\0/g and \\0?

\0 is the null character.
/\0/g is a pattern that will match all instances of the null character.
"\\0" is a string that will be displayed as "\0", since the first backslash acts as an escape character for the second backslash.
So this line of code replaces all instances of the null character (which is normally unreadable, unless you use a hex viewer) in the string input and replaces them with the human-readable string "\0", then stores the result in the string str.

It replaces null characters (\0 - Unicode 0x0) in the string with a backslash (\)followed by a 0.
var s = "asd0asd\x00asd";
console.log(s);
s = s.replace(/\0/g, "\\0");
console.log(s);
And the output is:
asd0asd�asd
asd0asd\0asd

Related

Why does String(value).replace(/[^0-9.-]/g, '') work?

I saw String(value).replace(/[^0-9.-]/g, '') from a library that described to filter illegal characters, and finally it will return the legal number. Why does this work and what will really be replaced? I feel confused because I think nothing will be replaced.
For example:
String("1.absd").replace(/[^0-9.-]/g, '')
// '1.'
String(value) will make string from value. So if you use number, this will convert it to string, because function replace is defined only from string.
replace(/[^0-9.-]/g, '') this will replace any matching given regular expression with empty string. In regular expression you have [^0-9.-]. The ^ at start means, that any characters not from this interval will match. So any character other than number, dot or -. Modifier g after slash means, that this regular expression is global and will be applied for all matches
so from "1.absd" the characters "absd" will be replaced with empty string

Backslash bug in JavaScript

I have a string that involves tricky \\ characters.
Below is the initial code, and what I am literally trying to achieve but it is not working. I have to replace the \" characters but I think that is where the bug is.
var current = csvArray[0][i].Replace("\"", "");
I have tried the variation below but it is still not working.
var current = csvArray[0][i].Replace('\"', '');
It is currently throwing an Uncaught TypeError: csvArray[0][i].Replace is not a function
Is there a way for Javascript to take my string ("\"") literally like in C#? Kindly help me investigate. Thanks!
If the sequence you want to match is a single backslash character followed by a quotation mark, then you need to escape the backslash itself because backslashes have special meaning in string literals. You then need to separately escape the quotation mark with its own backslash:
.replace("\\\"", "")
I believe that would also be true in C#.
Or you can simplify it by using single quotes around the string so that only the backslash needs to be escaped:
.replace('\\"', '')
If the first argument to .replace() is a string, however, it will only replace the first occurrence. To do a global replace you have to use a regular expression with the g flag, noting that backslashes need to be escaped in regular expressions too:
.replace(/\\"/g, '')
I'm not going to setup a demo array to exactly match your code, but here's a simple demo where you can see that a lone backslash or quote in the input string are not replaced, but all backslash-quote combinations are replaced:
var input = 'Some\\ test" \\" text \\" for demo \\"'
var output = input.replace(/\\"/g, '')
console.log(input)
console.log(output)

Backslash escape in indexOf

Why does n give 0 in the following instance :
var str = '\\nvga032.bmwgroup.net\QXE7868\Daten\IE\3_bookmarks.zzz'
var n = str.indexOf("\\");
alert(n) //0
Surely the escape character for a backslash is
'\\'
Am I missing something? I am looking for a single backslash at the last position. I tried lastIndexOf as well, and that also gives zero. Are the two '.'s messing things up?
indexOf matches on the string not the JavaScript source code used to create it.
A \ character starts an escape sequence.
\\ is the escape sequence for "A backslash".
The string assigned to str starts with \\ which puts a backslash in position 0 in the data.
The string passed to indexOf consists entirely of \\ which matches the first backslash in the data.
If you wanted to describe an escape sequence in a string you would use \\\\ (i.e. the escape sequence for a backslash followed by another escape sequence for a backslash resulting in data consisting of two backslashes).
"\\" will be parsed down to a single blackslash. And then indexOf will look for that single backslash, which happens to be at the start of the string (n=0).
If you want to search for TWO backslashes, you'll have to indexOf("\\\\") (FOUR backslashes, which will be parsed down to two literal backslashes).
Your "str" variable most probably doesn't contain what you expected. Write instead:
var str = '\\\\nvga032.bmwgroup.net\\QXE7868\\Daten\\IE\\3_bookmarks.zzz'
var n = str.lastIndexOf("\\");

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 \.
(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.
Try String.raw method:
str = String.raw`\apple` // "\apple"
Reference here: String.raw()
\ is an escape character, when followed by a non-special character it doesn't become a literal \. Instead, you have to double it \\.
console.log("\apple"); //-> "apple"
console.log("\\apple"); //-> "\apple"
There is no way to get the original, raw string definition or create a literal string without escape characters.
please try the below one it works for me and I'm getting the output with backslash
String sss="dfsdf\\dfds";
System.out.println(sss);

questions on javascript - string.replace()

I was trying to run this line of code and wondering why it is not working. Does anyone has an answer for this?
var string = "foo bar";
string = string.replace(" ", "");
alert(string.length);
why issit that the length of the string is not changed to 6 instead?
The function only replaces one instance of the string you search for.
To replace more, you can match with a regular expression:
string = string.replace(/\s+/g, '');
That strips all "whitespace" characters. The "\s" matches whitespace, the "+" means "one or more occurrences" of whitespace characters, and the trailing "g" means "do it to all matching sequences in the string".
Because you have more than one space in your string and the .replace is replacing one space, the first one encountered.
This works as expected, with only one space
var string = "foo bar";
string = string.replace(" ", "");
alert(string.length);
replace, when passed a string as the first parameter, only replaces the first occurrence of that string. To replace everything, you'd need a regular expression:
alert("foo bar".replace(/ /g, ""));
That's because only one space has been replaced. Per JavaScript 1.5 specification, String.replace() takes a regular expression as first parameter and the behavior for string parameters is undefined. Browsers later decided to treat strings similarly - but there is no way to specify g flag on a string, so only one replacement is done. This will do what you want:
string = string.replace(/ /g, '');
The version provided by Pointy (/\s+/g) might be more efficient however. And it will replace other types of whitespace (tabs, newlines) as well.

Categories