473,654 Members | 3,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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\folder 3\file.txt";

var myPat = /%5C/g; //this is using regular expression to
define the escaped version of a backslash

fileString = escape(fileStri ng);
fileString = fileString.repl ace(myPat,"%5C% 5C");
fileString = unescape(fileSt ring);

fileString now equals "\\\\mydev\\fol der\\folder2\\f older3\
\file.txt"

Hope this keeps someone else from pounding their heads against the
monitor. Enjoy!

May 17 '07 #1
9 11451
On May 17, 7:49 pm, i...@indigotea. com 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\folder 3\file.txt";

var myPat = /%5C/g; //this is using regular expression to
define the escaped version of a backslash

fileString = escape(fileStri ng);
fileString = fileString.repl ace(myPat,"%5C% 5C");
fileString = unescape(fileSt ring);

fileString now equals "\\\\mydev\\fol der\\folder2\\f older3\
\file.txt"
What browser? Did you try this?

fileString.repl ace(/\\/g,"\\\\");

---
Geoff

May 18 '07 #2
in**@indigotea. com said the following on 5/17/2007 7:49 PM:
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\folder 3\file.txt";

var myPat = /%5C/g; //this is using regular expression to
define the escaped version of a backslash

fileString = escape(fileStri ng);
fileString = fileString.repl ace(myPat,"%5C% 5C");
fileString = unescape(fileSt ring);

fileString now equals "\\\\mydev\\fol der\\folder2\\f older3\
\file.txt"

Hope this keeps someone else from pounding their heads against the
monitor. Enjoy!
What I get, in IE7 and FF2.0, for that code isn't even close to what you
say it equals.

document.write( fileString) produces:
\\mydev older older2 older3 ile.txt

IE7 window.clipboar dData.setData(' Text',fileStrin g); gives this:
\\mydev older older2 older3 ile.txt

The boxes in the clipboard appear as some whacked out funny looking
graphic (that I think is neat looking) but won't copy/paste into
Thunderbird for me.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 19 '07 #3
Geoffrey Summerhayes said the following on 5/18/2007 3:40 AM:
On May 17, 7:49 pm, i...@indigotea. com 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\folder 3\file.txt";

var myPat = /%5C/g; //this is using regular expression to
define the escaped version of a backslash

fileString = escape(fileStri ng);
fileString = fileString.repl ace(myPat,"%5C% 5C");
fileString = unescape(fileSt ring);

fileString now equals "\\\\mydev\\fol der\\folder2\\f older3\
\file.txt"

What browser? Did you try this?

fileString.repl ace(/\\/g,"\\\\");
var fileString = "c:\new folder";
fileString.repl ace(/\\/g,"\\\\");
alert(fileStrin g)

Did *you* test it?

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 19 '07 #4
On May 19, 1:54 am, Randy Webb <HikksNotAtH... @aol.comwrote:
Geoffrey Summerhayes said the following on 5/18/2007 3:40 AM:
On May 17, 7:49 pm, i...@indigotea. com 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\folder 3\file.txt";
var myPat = /%5C/g; //this is using regular expression to
define the escaped version of a backslash
fileString = escape(fileStri ng);
fileString = fileString.repl ace(myPat,"%5C% 5C");
fileString = unescape(fileSt ring);
fileString now equals "\\\\mydev\\fol der\\folder2\\f older3\
\file.txt"
What browser? Did you try this?
fileString.repl ace(/\\/g,"\\\\");

var fileString = "c:\new folder";
fileString.repl ace(/\\/g,"\\\\");
alert(fileStrin g)

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.

Also, you've used only a string replacement expression, without
assigning the result. Therefore the alert will present the string
without the effect of the replacement operation, i.e, a representation
of the original internal version.

--
../rh


May 19 '07 #5
ro********@gmai l.com said the following on 5/19/2007 3:23 PM:
On May 19, 1:54 am, Randy Webb <HikksNotAtH... @aol.comwrote:
>Geoffrey Summerhayes said the following on 5/18/2007 3:40 AM:
>>On May 17, 7:49 pm, i...@indigotea. com 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\folder 3\file.txt";
var myPat = /%5C/g; //this is using regular expression to
define the escaped version of a backslash
fileString = escape(fileStri ng);
fileString = fileString.repl ace(myPat,"%5C% 5C");
fileString = unescape(fileSt ring);
fileString now equals "\\\\mydev\\fol der\\folder2\\f older3\
\file.txt"
What browser? Did you try this?
fileString.re place(/\\/g,"\\\\");
var fileString = "c:\new folder";
fileString.rep lace(/\\/g,"\\\\");
alert(fileStri ng)

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.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 19 '07 #6
On May 19, 4:32 pm, Randy Webb <HikksNotAtH... @aol.comwrote:
ron.h.h...@gmai l.com said the following on 5/19/2007 3:23 PM:
On May 19, 1:54 am, Randy Webb <HikksNotAtH... @aol.comwrote:
Geoffrey Summerhayes said the following on 5/18/2007 3:40 AM:
<..>
>What browser? Did you try this?
fileString.rep lace(/\\/g,"\\\\");
var fileString = "c:\new folder";
fileString.repl ace(/\\/g,"\\\\");
alert(fileStrin g)
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.
Correct! Did *you* test it? ;-)
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.
Agreed, but that's only because of the choice of the initial test
string that has no backslash to be replaced. It remains a faulty test
which is what I presumed it was to be.

<..>

The point is that

fileString.repl ace(/\\/g,"\\\\");

when assigned should give the desired result in a more direct fashion
than the process given by the OP. No?

--
../rh
May 20 '07 #7
On May 19, 4:54 am, Randy Webb <HikksNotAtH... @aol.comwrote:
Geoffrey Summerhayes said the following on 5/18/2007 3:40 AM:
What browser? Did you try this?
fileString.repl ace(/\\/g,"\\\\");

var fileString = "c:\new folder";
fileString.repl ace(/\\/g,"\\\\");
alert(fileStrin g)
I wrote half a line of code and you
criticize it based on a half-built
test that is already fatally flawed
no matter what method you use, try:

var fileString = "file:\\\\c:\\n ew folder\\test\\" ;
alert(fileStrin g); // print original string for comparison!!!
alert(fileStrin g.replace(/\\/g,"\\\\"); // print result of replace

---
Geoff

May 20 '07 #8
Geoffrey Summerhayes said the following on 5/20/2007 4:28 PM:
On May 19, 4:54 am, Randy Webb <HikksNotAtH... @aol.comwrote:
>Geoffrey Summerhayes said the following on 5/18/2007 3:40 AM:
>>What browser? Did you try this?
fileString.re place(/\\/g,"\\\\");
var fileString = "c:\new folder";
fileString.rep lace(/\\/g,"\\\\");
alert(fileStri ng)

I wrote half a line of code
That you didn't thoroughly test.
and you criticize it based on a half-built
test that is already fatally flawed
no matter what method you use,
My test was very well thought out and designed to specifically show a
flaw in both approaches. Typically, when a string that contains a file
path needs to be escaped it is because it is not already escaped. If
that string comes from a file input (which some browsers won't let you
read the .value of), then you can not get back to the original if it
contains certain characters in it. My fileString was designed with that
in mind to show that it is not a trivial task.
try:

var fileString = "file:\\\\c:\\n ew folder\\test\\" ;
Why does that string need escaping though? It is already escaped.

It is easy to come up with a test case to show that a particular piece
of code will work. But, when that piece of code can be shown to be
flawed when presented with another piece of string code then the
original is still flawed.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 20 '07 #9
On May 20, 4:47 pm, Randy Webb <HikksNotAtH... @aol.comwrote:
Geoffrey Summerhayes said the following on 5/20/2007 4:28 PM:
On May 19, 4:54 am, Randy Webb <HikksNotAtH... @aol.comwrote:
Geoffrey Summerhayes said the following on 5/18/2007 3:40 AM:
>What browser? Did you try this?
fileString.rep lace(/\\/g,"\\\\");
var fileString = "c:\new folder";
fileString.repl ace(/\\/g,"\\\\");
alert(fileStrin g)
I wrote half a line of code

That you didn't thoroughly test.
WTF? I'm sorry, when you were peering
over my shoulder, you should have said
something, I didn't see you here.
and you criticize it based on a half-built
test that is already fatally flawed
no matter what method you use,

My test was very well thought out and designed to specifically show a
flaw in both approaches. Typically, when a string that contains a file
path needs to be escaped it is because it is not already escaped. If
that string comes from a file input (which some browsers won't let you
read the .value of), then you can not get back to the original if it
contains certain characters in it. My fileString was designed with that
in mind to show that it is not a trivial task.
Pfui. All your test showed was that the GIGO
rule still holds. Putting an alert box showing
the original string before processing would
have a more to the point, and a little more
intellectually honest, because it would have
shown that the input wasn't the *desired* path
string.
var fileString = "file:\\\\c:\\n ew folder\\test\\" ;

Why does that string need escaping though? It is already escaped.
Imprecise. It's escaped in the source only.
fileString will hold the unescaped string,
it's length will be 26 NOT 31.

Look, why have the replace in the first
place? Well, when I write a web page to
display HTML, I don't type <body>, I type
&lt;body&gt; . It displays properly, can be
cut and pasted from the page to an editor.
Do people write functions/regexps to do
this kind of translation? Sure.

The only credible reason for having this is
to create a string that can be eval'd or
cut-and-pasted into javascript source, or some
other language that follows the same escaping
rules. A poor man's *PRINT_READABLY * (Common
Lisp), specific to pathnames.

var foo="C:\\somepa th";
var bar=eval("'"+fo o+"'");
var baz=eval("'"+fo o.replace(/\\/g,"\\\\")+"'" );
alert('foo = bar: '+(foo==bar)+'\ nfoo = baz: '+(foo==baz));

displays

foo = bar: false
foo = baz: true
It is easy to come up with a test case to show that a particular piece
of code will work. But, when that piece of code can be shown to be
flawed when presented with another piece of string code then the
original is still flawed.
As I said above, if you feed it junk, you
get junk back. That's not flaw in the tool,
it's a mistake on the user's part.

---
Geoff

May 22 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
4992
by: Margaret MacDonald | last post by:
I've been going mad trying to figure out how to do this--it should be easy! Allow the user to enter '\_sometext\_', i.e., literal backslash, underscore, some text, literal backslash, underscore and, after submitting via POST to a preg_replace filter, get back '_sometext_' (i.e., the same thing with the literal backslashes stripped)
6
24517
by: Mikheil | last post by:
Hello! I need to translate file destination name with one backslashes "c:\program files\directory\file.txt" to string containing double backslashes "c:\\program files\\directory\\file.txt" If there is a nativi function on C++, or any algolithms I'd be glad to know about them. Thank you
1
2975
by: Alastair Cameron | last post by:
VB6, MSXML 3.2 installed: Q1. I am having a problem selecting nodes with XPATH expressions when an attribute values contain backslashes (\\) in as part of its value: For example the following statement fails to find a node (even though one exists in the XML) if the value of the LDAPServerURL attribute is \\LocalServer but works if the value is "LocalServer".
4
1467
by: Stride | last post by:
Hey all, I'm not familiar with javascript all that much, but I was at yahoo's site and noticed that when you click on say directory or some other search category it does that neat little switching thing. Now I'm a curious individual so I took a look at the source for it, and the relevant code goes: <a id=v4 class=o href="r/0a/*-http://search.yahoo.com/search/dir" title="Search Directory"> Now as far as I've been able to figure out...
0
949
by: Matt Hamilton | last post by:
I have a GridView control that has a boundfield. When a user saves a backslash in the text and returns to the GridView there is a javascript error (IE 6 - Unterminated string constant). The error displays with or without the HtmlEncode="true" property on the boundfield. How can I allow display of backslashes in a bound GridView without a javascript error?
2
3518
by: wylbur37 | last post by:
When using a form with an input textbox such as the following ... <input type="text" name="field1" size=30> I discovered that when a backslash (\) is typed into the textbox, when I later check the value (in $field1), I get *two* backslashes. For example, If I type ... c:\abc\xyz
1
1541
The1corrupted
by: The1corrupted | last post by:
Okay, this is somewhat annoying but I need to escape backslashes to make something look nice. <?php if ($mesgread=="/" AND $user!=$otheruser) { $action=explode(" ", $msg, 2); $_SESSION=$_SESSION.$action."<br>"; echo $META }
3
13486
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
OpenFileDialog gives me the following, which I place in tbDevPath.Text: x:\\myVob\\mySolution\\mySolution.sln I really need this path to be single-backslashes, not double-backslashes, so I tried the following: string updatedDevPath = Regex.Replace(tbDevPath.Text, @"^\\\\$", "\\") This still leaves me with double-backslashes. Any suggestions?
7
13013
by: JohnF | last post by:
I have a function textag($expression){...} whose $expression argument is a string that can contain substrings like \alpha with one backslash or like a&b\\c&d with two backslashes. If I write <?php textag('\alpha'); ?with the expression argument in single quotes, then that works fine, and the single backslash isn't interpreted or changed, which is what I want. But if I write <?php textag('a&b\\c&d'); ?>
0
8376
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8489
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8594
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7307
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2716
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.