Replacing colon by double backslash - javascript

I need to replace : with double backslash \\ but the code below is ignoring one slash.
var original_id = $j(element).attr('id'); // e.g. sub:777
var new_id = original_id.split(":");
new_id = new_id.join("\\:");
alert(new_id);
Instead of displaying sub\\:777, sub\:777 is displayed. The code is ignoring one \ slash.
I would appreciate it if somebody could show me my error.

You must escape the backslashes:
new_id = new_id.join("\\\\:");
See JavaScript Special Characters for some details.

\ is used as an escape character in many languages for things like \n for new line. The reason why you see one is because it is escaped by the first \. (otherwise it would be invisible to you). To remedy this, escape two \s like so: "\\\\:"

Related

Javascript substring from last backslash char

This is probably a basic syntax question, but I am not able to find a syntax for a backslash character. Following and other syntaxes that I tried are not accepted for this char.
var fileNameSubstring = data.FileName.substring(data.FileName.lastIndexOf('\') + 1, data.FileName.length);
When defining a string in Javascript you can use the backslash (called escape character) to indicate special characters like new line \n.
To actually have a backslash in your string you should use double blackslash \\.
var fileNameSubstring = data.FileName.substring(data.FileName.lastIndexOf('\\') + 1, data.FileName.length);

How to replace single backslash with double backslash in Javascript

I have seen a lot of duplicates but none of them works
I spend a day to get this thing up. Gone through all duplicates
I need to replace a single backslash with a double backslash.
I am able to replace all other characters with a double backslash.
console.log("hello & 100\|".replace(/([&%$#_{}])/g, "\\"));
I know that two backslashes is indeed used to make a single backslash.
I tried all the possible ways of using regex as well. But neither works.
Here is another snippet I am using.
var replaceableString = "A\B";
replaceableString = replaceableString.replace(/\\/g, "\\\\");
console.log(replaceableString);
Unfortunately, this also is not working.
Please share if there are any workarounds.
var str = "AAA\\BBB\\CCC\\DDD\\EEE";
var rgx = /\\/g;
var res = str.replace(rgx, "\\\\");
console.log(res);
Comments below questions are very revealing. Single backslash \ is escape character. To obtain single backslash we must use double backslashes. So we must use four backslashes(in reality double) for replace the backslash.

How to remove forward and backward slashes from string in javascript

I want to remove all the forward and backward slash characters from the string using the Javascript.
Here is what I have tried:
var str = "//hcandna\\"
str.replace(/\\/g,'');
I also tried using str.replace(/\\///g,''), but I am not able to do it.
How can I do it?
You can just replace \/ or (|) \\ to remove all occurrences:
var str = "//hcandna\\"
console.log( str.replace(/\\|\//g,'') );
Little side note about escaping in your RegEx:
A slash \ in front of a reserved character, is to escape it from it's function and just represent it as a char. That's why your approach \\// did not make sense. You escapes \ with \, so it becomes \\. But if you want to escape /, you need to do it like this too: \/.
You want something more like this:
var str = "//hcandna\\"
str=str.replace(/[\/\\]/g,'');
console.log(str);
This will search for the set of characters containing a forward or backward slash and replace them globally. What you had requires a backslash followed by a forward slash.
Here's the output from Node:
str.replace(/[\/\\]/g,'')
'hcandna'
You need to add the result to a new string like:
var newstr = str.replace(/(\\|\/)+/ig, '');
You can use this code snippet
str.replace(/(\\|\/)/g,'');

Regexp adds invisible dot character replacing \b

I want this to be my regex: /^word\b/ (word is dynamic)
When I set it up to be dynamic I have to use this:
var word='spoon';
'spoon .table .chair'.match(new RegExp('^'+word+'\b'));
However, this finds null, while this:
var word='spoon';
'spoon .table .chair'.match(/^spoon\b/);
finds ["spoon"].
The interesting part is when I examine the difference between the regex I worte and the regex RegExp wrote:
console.log(/^spoon\b/,new RegExp('^'+word+'\b'))
It shows this:
/^spoon\b/ /^spoon/
If I then copy the second part of the log output (/^spoon/) into my code editor I see this character:
What is that? How do I do RegExp word-ending-with as I am not always guaranteed to have a space at the end when the string might be a one-word string (spoon or another word)
I'd rather just do this without the invisible thing
You've got to escape the \ in the b in the regex string by adding an extra slash:
var regex = new RegExp('^' + word + '\\b')
This is because the RegExp is expecting to see the two characters \ and b, but the string '\b' is one character, ascii 8, the backspace character (in the same way that '\n' is a single newline character).
In Javascript, \b doesn't mean a \ followed by a b. It means the backspace character (ASCII code 8). To get a \ followed by a b, you need to escape the slash so that Javascript doesn't parse it as a backspace:
'^' + word + '\\b'
The same thing applies if you want to use \d or \s or anything else: You need to escape the \ with another one so that Javascript doesn't think it's a Javascript escape code and the RegExp can parse it as what you expect.

Replace '\' with '-' in a string

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 -

Categories

Resources