Replace double backslashes with a single backslash in javascript
This should do it: "C:\\backup\\".replace(/\\\\/g, '\\')
In the regular expression, a single \
must be escaped to \\
, and in the replacement \
also.
[edit 2021] Maybe it's better to use template literals.
console.log(`original solution ${"C:\\backup\\".replace(/\\\\/g, '\\')}`)
// a template literal will automagically replace \\ with \
console.log(`template string without further ado ${`C:\\backup\\`}`);
// but if they are escaped themselves
console.log(`Double escaped ${`C:\\\\backup\\\\`.replace(/\\\\/g, '\\')}`);
// don't want to replace the second \\
console.log(`not the second ${`C:\\\\backup\\\\`.replace(/\\\\/, '\\')}`);
// don't want to replace the first \\
console.log(`not the first ${`C:\\\\backup\\`.replace(/[\\]$/, '\\')}`);
Replace double backslash with single backslash in JavaScript
When using regex,
a literal reverse solidus (a.k.a. backslash,( a.k.a.
\
)) must be escaped with a\
.So when you input
\\
, that's literally\
.If you want to input 2
\
each one must be escaped. That means\\\\
is literally a string of 2\
.
The following demo uses ES6 Template Literals:
It's a new syntax for String that uses the backtick ` (top left corner key) instead of quotes " or '.
It can interpolate variables by prefixing with
${
and suffixing with}
.var x = 2; `this template literal will display x as ${x}.`
// this template literal will display x as 2.The
\
isU+5c
in unicode. Using escaped unicode in Template Literal, it would be prefixed with\u{
and suffixed with}
`A template literal with an escaped unicode backslash is \u{5c}`
// A template literal with an escaped unicode backslash is \.
Demo
function XtoY(str, x, y) { var rgx = `/${x}/g`; var result = str.replace(rgx, `${y}`); return result;}
const uni = `\u{5c}`;const string = `\\a\\a\\b`;const from = `${uni}${uni}`const to = `${uni}`;
let z = XtoY(string, from, to);
console.log(z);
Replace double backslashes with single backslash
That Replace
in your code is doing nothing because there are no double backslashes in your string... As others have already pointed out, it's just a matter of debugger visualization so you can copy that and use it in your code, for example.
So, if you do this:
You see the double backslash there but it's not in the actual string, of course. See what you get in the console:
My advice is that you simulate a double click on the selected file by running this:
System.Diagnostics.Process.Start(filetoplay);
It should open your default mp3 player. I think it will give you an error due to missing file, wrong format or something. If it plays, then the bug is on the player part of your code and you can stop worrying about the double slashes. :)
Replace two backslashes with a single backslash
Your output doesn't have double backslashes. What you are looking at is the repr()
value of the string and that displays with escaped backslashes. Assuming your temp_folder
would have double backslashes, you should instead use:
print(temp_folder.replace('\\\\', '\\'))
and that will show you:
C:\Users\User\AppData\Local\Temp
which also drops the quotes.
But your temp_folder
is unlikely to have double backslashes and this difference in display probably got you thinking that there are double backslashes in the return value from tempfile.gettempdir()
. As @Jean-Francois indicated, there should not be (at least not on Windows). So you don't need to use the .replace()
, just print:
print(temp_folder)
Replace double backslashes with single backslash using JS223 processor
Ok I resolved this by doing a .toString().replace("\\\\","\\").
How to replace double backslash to single backslash
a = 'RXIE-SERVER\\MSSQLSERVER_NEW'
doesn't have a double backslash. It has an escaped single backslash, it's just safer (and will eventually be required) to escape it so Python doesn't think \M
is an attempt at a string escape. If you do:
print(a)
you'll see it only prints one backslash (because print
outputs the raw data without showing escapes).
The reason a.replace('\\', '')
doesn't work is because it replaced the single backslash with nothing (and it would do so for all backslashes); a.replace('\\\\', '\\')
doesn't work because '\\\\'
represents the actual doubled backslash, and you don't have any of those.
If your input came from some other source (not the literal you described) and actually has a doubled-backslash, then a.replace('\\\\', '\\')
actually worked, but REPL's echo the repr
of the object, and for str
, that means adding the backslash escape to make it a legal, equivalent str
literal, so it looked like a double-backslash, but only had one. If you change >>> a.replace('\\\\', '\\')
to >>> print(a.replace('\\\\', '\\'))
(which prints the human-friendly form, not the repr
), you'll see it display only a single backslash.
If you don't like how it looks in your code, use raw strings to remove the need for the escape:
a = r'RXIE-SERVER\MSSQLSERVER_NEW'
# ^ note prefix that makes it raw
Related Topics
How to Solve Maven 2.6 Resource Plugin Dependency
Sharing a Variable Between Multiple Different Threads
Spring Data JPA Saveall Not Doing Batch Insert
Http Post Request With Content Type Application/X-Www-Form-Urlencoded Not Working in Spring
Default Value in Lombok. How to Init Default With Both Constructor and Builder
Custom Pagination by Array List
Jsr 303 Validation, If One Field Equals "Something", Then These Other Fields Should Not Be Null
Turkish Identity Number Verification
How to Input Date Using Sendkeys in Selenium Webdriver
How to Save a File Downloaded With Httpclient into a Specific Folder
Handling the Null Value from a Resultset
I Want to Get Time Difference Between Two Time in Milisecond
Using Nested While Loop to Print Pyramid of Stars
How to Exit an Android App Programmatically
How to Post Data in an Array in Android Using Retrofit 2
Array Pairwise Matching in Java Give Error Also Store Data Between Two Similar Element