Re: Doubling backslashes in JavaScript - it's unorthodox, but it works...
- From: Randy Webb <HikksNotAtHome@xxxxxxx>
- Date: Sat, 19 May 2007 19:32:37 -0400
ron.h.hall@xxxxxxxxx said the following on 5/19/2007 3:23 PM:
On May 19, 1:54 am, Randy Webb <HikksNotAtH...@xxxxxxx> wrote:Geoffrey Summerhayes said the following on 5/18/2007 3:40 AM:
On May 17, 7:49 pm, i...@xxxxxxxxxxxxx wrote:var fileString = "c:\new folder";I've spent the last two hours trying every other solution listed, toWhat browser? Did you try this?
no avail: this works, so I'm sharing it for the other folks who
couldn't find a solution other than switching to another language.
var fileString = "\\mydev\folder\folder2\folder3\file.txt";
var myPat = /%5C/g; //this is using regular expression to
define the escaped version of a backslash
fileString = escape(fileString);
fileString = fileString.replace(myPat,"%5C%5C");
fileString = unescape(fileString);
fileString now equals "\\\\mydev\\folder\\folder2\\folder3\
\file.txt"
fileString.replace(/\\/g,"\\\\");
fileString.replace(/\\/g,"\\\\");
alert(fileString)
Did *you* test it?
The first thing to note is that fileString, as you've presented,
doesn't actually contain a backslash at the time of the attempt to
perform replacement. That's because a backslash in a string literal is
an escape character. In this case, since the backslash precedes an
"n", it results in a newline character being generated when converting
the string to its internal form.
And testing, if not directly, would have indirectly caused that to be discovered. If you alert fileString before the replace, the results should make one wonder why before continuing.
Also, you've used only a string replacement expression, without
assigning the result.
True, but, assigning it to another variable had no impact on what was being written/alerted.
Therefore the alert will present the string without the effect of
the replacement operation, i.e, a representation of the original internal version.
Even then, it is always better to have the server escape it. By the time the file path gets to the browser it is too late to try to escape it.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
.
- Follow-Ups:
- Re: Doubling backslashes in JavaScript - it's unorthodox, but it works...
- From: ron . h . hall
- Re: Doubling backslashes in JavaScript - it's unorthodox, but it works...
- References:
- Doubling backslashes in JavaScript - it's unorthodox, but it works...
- From: indi
- Re: Doubling backslashes in JavaScript - it's unorthodox, but it works...
- From: Geoffrey Summerhayes
- Re: Doubling backslashes in JavaScript - it's unorthodox, but it works...
- From: Randy Webb
- Re: Doubling backslashes in JavaScript - it's unorthodox, but it works...
- From: ron . h . hall
- Doubling backslashes in JavaScript - it's unorthodox, but it works...
- Prev by Date: Re: question about Timers @ stuff............
- Next by Date: Re: Subject changed to: Windows Update and non-IE browsers
- Previous by thread: Re: Doubling backslashes in JavaScript - it's unorthodox, but it works...
- Next by thread: Re: Doubling backslashes in JavaScript - it's unorthodox, but it works...
- Index(es):
Relevant Pages
|
Loading