Replace '\' with '-' in a string - javascript

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 -

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);

Javascript replace character with backslash

How do I replace + with \+ using the replace method?
console.log("abc+pqr".replace("+","\+"));
This gives the original string abc+pqr but I want abc\+pqr.
How do I get this?
You need to escape the backslash:
console.log("abc+pqr".replace("+","\\+"));

How to Remove double backslashes in a string to Single one?

My string is tel:\\99999999999. How i can replace '\' to '\' single? I want output like: tel:\99999999999.
Please see attached image backslashes not showing into question.
You should escape the slash twice:
"tel:\\99999999999".replace('\\\\', '\\');
You can simply use tel.replace(/\\\\/g, "\\").
Demo:
var tel ="\\99999999999";
console.log(tel.replace(/\\\\/g, "\\"));
You need to escape the \ character with another \, because it's an escape character in JavaScript, you can check JavaScript backslash (\) in variables is causing an error for further reading.
use .replace() to achieve what you want, str.replace("\\\\","\\")
var str = "tel:\\99999999999";
console.log(str.replace("\\\\","\\"))
You can do like this
"tel:\\99999999999".replace('\\\\', '');

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.

Replacing colon by double backslash

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: "\\\\:"

Categories

Resources