简体   繁体   中英

Replace single backslash “\” with double backslashes “\\”

I have string with file path. I want to replace all single backslashes ("\\") with double backslashes ("\\\\").

   var replaceableString = "c:\asd\flkj\klsd\ffjkl";
   var part = /@"\\"/g;
   var filePath = replaceableString .replace(part, /@"\\"/);
   console.log(filePath);

Console showed me it.

   c:asdlkjklsdfjkl

I found something like this, unfortunately it didn't work. Replacing \\ with \\\\

Try:

   var parts = replaceableString.split('\\');
   var output = parts.join('\\\\');

Personally, as I am not so expert in reg exps, I tend to avoid them when dealing with non-alphanumeric characters, both due to readability and to avoid weird mistake.

var replaceableString = "c:\asd\flkj\klsd\ffjkl";
alert(replaceableString);

This will alert you c:asdlkjklsdfjkl because '\\' is an escape character which will not be considered.

To have a backslash in your string , you should do something like this..

var replaceableString = "c:\\asd\\flkj\\klsd\\ffjkl";
alert(replaceableString);

This will alert you c:\\asd\\flkj\\klsd\\ffjkl

JS Fiddle

Learn about Escape sequences here

If you want your string to have '\\' by default , you should escape it .. Use escape() function

var replaceableString = escape("c:\asd\flkj\klsd\ffjkl");
alert(replaceableString);

JS Fiddle

You have several problems in your code.

  1. To get a \\ in your string variable you need to escape it.

    When you create a string like this: replaceableString = "c:\\asd\\flkj\\klsd\\ffjkl"; characters with a \\ before are treated as escape sequences. So during the string creation, it tries to interpret the escape sequence \\a , since this is not valid it stores the a to the string. Eg \\n would have been interpreted as newline.

  2. I assume the @ is coming from a .net example. Javascript does not know "raw" strings.

  3. remove the quotes from your regex.

This would do what you want:

var string = "c:\\asd\\flkj\\klsd\\ffjkl";
var regex = /\\/g;
var FilePath = string.replace(regex, "\\\\");

Here is the answer:

For replacing single backslash with single forward slash :

var stringReplaced = String.raw`c:\asd\flkj\klsd\ffjkl`.split('\\').join('/')
console.log(stringReplaced);

For replacing double backslash with single forward slash :

var stringReplaced = String.raw`c:\\asd\\flkj\\klsd\\ffjkl`.split('\\\\').join('/')
console.log(stringReplaced);

\\ is a escape character. Therefore replaceableString does not contain any backslashes.

To fix this you should declare the string like this:

var replaceableString = "c:\\asd\\flkj\\klsd\\ffjkl";

First encode the string

then replace all occurrences of %5C with %5C%5C

At the end decode the string

var result = encodeURI(input);
result=decodeURI(result.replace(/%5C/g,"%5C%5C"));

If you have no control over the contents of the string you are trying to find backslashes in, and it contains SINGLE \\ values (eg. variable myPath contains C:\\Some\\Folder\\file.jpg ), then you can actually reference the single backslashes in JavaScript as String.fromCharCode(92) .

So to get the file name in my filepath example above.

var justTheName = myPath.split(String.fromCharCode(92)).pop();

In case of string matching, it is better to use encodeURIComponent, decodeURIComponent.

match(encodeURIComponent(inputString));

function match(input)
{

for(i=0; i<arr.length; i++)
{
if(arr[i] == decodeURIComponent(input))
return true;
else return false;
}
}

In the case of a single back slash in the string, the javascript replace method did not allow me to replace the single back slash.

Instead I had to use the split method which returns an array of the split strings and then concatenate the strings without the back slash (or whatever you want to replace it with)

Solution (replaced backslash with underscore):

var splitText = stringWithBackslash.split('\\');
var updatedText = splitText[0] + '_' + splitText[1];

You need to pass to pass value of a string through String.raw before you assign value to a variable.

 var replaceableString = String.raw`c:\\asd\\flkj\\klsd\\ffjkl`.replace(/\\\\/g,"\\\\\\\\"); console.log(replaceableString)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM