23

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

4

10 回答 10

11

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.

于 2013-04-22T09:55:13.420 回答
11
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

于 2013-04-22T09:55:57.800 回答
9

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, "\\\\");
于 2013-04-22T09:56:37.110 回答
6

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);
于 2019-01-14T09:35:49.987 回答
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";
于 2013-04-22T09:55:18.063 回答
3

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"));
于 2016-08-29T12:54:57.597 回答
0

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();
于 2014-03-25T04:54:07.060 回答
0

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;
}
}
于 2014-05-12T14:52:22.010 回答
-1

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];
于 2016-05-11T20:03:48.083 回答
-2

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)

于 2021-04-12T21:44:40.550 回答