Replace single backslash “\\” with double backslas

2019-01-27 13:42发布

问题:

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

回答1:

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



回答2:

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. E.g. \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, "\\\\");


回答3:

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.



回答4:

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


回答5:

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


回答6:

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


回答7:

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


回答8:

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


回答9:

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