Re: Doubling backslashes in JavaScript - it's unorthodox, but it works...



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:
I've spent the last two hours trying every other solution listed, to
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"
What browser? Did you try this?
fileString.replace(/\\/g,"\\\\");
var fileString = "c:\new folder";
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/
.



Relevant Pages

  • Re: Unrecognized escape sequences in string literals
    ... character without knowing the context, ... '\n' maps to the string chr. ... In both cases the backslash in the literal have the same meaning: ... escape every backslash, ...
    (comp.lang.python)
  • Re: Unrecognized escape sequences in string literals
    ... how unrecognized escape sequences are treated in Python. ... a backslash is just an ordinary character, ... Why should a backslash in a string literal be an error? ...
    (comp.lang.python)
  • Re: ADAM CSVDE/LDIFDE import of DNs with backslashes in them?
    ... I'm not sure if that will work, but essentially it is specifying an escape ... being migrated from a database into an ADAM directory. ... backslash is omitted). ... I'm focussing on trying to get the LDIFDE to work right now. ...
    (microsoft.public.windows.server.active_directory)

Loading