0

i have a var in javascript that looks like this:

C:\docs\path\file.exe

how can i replace \ with another char like ? so the var becomes

C:?docs?path?file.exe

edit
i am trying to find the size of a file in JS. the only way i managed to do it, is to call a [WebMethod] using $ajax. when i try to send the path as it is, i get an escape character error, so i chose to replace '\' by '?' and then the [WebMethod] replaces '?' with '\' check the file size and returns it.

2
  • 1
    i just need var to $.ajax argument. if i did not replace '\' i get an error saying: Unrecognized escape sequence Commented Apr 8, 2011 at 13:54
  • 1
    there is a better solution to this problem than replacing stuff. Show some more code. Where is that string coming from? Commented Apr 8, 2011 at 13:55

5 Answers 5

2

You can do this:

yourVar = yourVar.replace(/\\/g, '?');

The backslash has to be doubled in the regular expression because it's special in that syntax. Otherwise, it's pretty simple.

Sign up to request clarification or add additional context in comments.

4 Comments

I'll bet a beer there is a better solution to this problem than replacing characters. It looks like the \ es are getting misrepresented as escape characters, which can be fixed (downvote isn't mine though)
This doesn't present the problem that the string of yourVar has to use escaped characters as well.
The string value of "yourVar" would have to have the backslashes quoted if it got its value as a simple JavaScript assignment. If the string were typed by a user into a text input, or fetched from a server as an AJAX call, there would be no need for quoting as there would be no JavaScript string constant involved.
@Pekka you may be right, but my patience with people who ask questions best posed to mind readers is quite limited this morning :-)
0

escape the \ with \\ so

document.write(myPath.replace("\\", "?"));

2 Comments

This will replace only the first backslash.
How about telling the OP to do this on the original string so he doesn't have to do any replacing at all?
0
var path = "C:\\docs\\path\\file.exe"
path ; //# => C:\docs\path\file.exe
path = path.replace(/\\/g, '?');
path ; //# => C:?docs?path?file.exe

Comments

0
var path = "C:\\docs\\path\\file.exe";
path = path.replace(/\\/g,"?");
alert(path);

You need to escape the "\" characters for this to work properly.

4 Comments

Your path initializer literal contains a formfeed. I'm guessing you probably didn't mean for it to... ;-)
In a string literal, \f is a formfeed. You need to escape the backslashes in your initializer.
@T.J. Updated answer. Is that what you meant?
Yup, now the string contains backslashes.
-1

have u tried this ?

var str = "C:\docs\path\file.exe"
var newStr = str.replace(/\\/g,"?")

5 Comments

the "g" parameter is for apply this replace for all matched characters. without "g" (global) parameter it replaces only one time
@Simasher, @Shaz: It would work if the str initializer weren't incorrect. @Simasher, Shaz originally made the same mistake, see comments there. (Also, strongly recommend not relying on the horror that is automatic semicolon insertion.)
i don't understand? it worked for me! does this mean it can cause problems in the future?
@scatman The downvote is not mine, but you do not need any of this in the first place. Just escape each backslash with another backslash from the start.
@scatman: The relevant bit really is var newStr = str.replace(/\\/g,"?"). Presumably you already have the string, and the string already really has backslashes in it, so don't worry about the first line of the answer being wrong, it's not important. The replace line is the important bit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.