Problem with escape characters when splitting string into path/filename

Posted on 2008-06-18
Last Modified: 2008-09-20
I am trying to write a small bit of javascript which splits the path from a variable containing path/filename.  The problem is that the HTML is embedded in an app that returns the path/filename using a special token, and when it is returned, the path/filename contains only a single \ between folders like:


instead of a escaped double \\ like:


I really just dabble in javascript, and need some assistance extracting just the path.  I am using the attached code. Note that '{DOC:FILENAME}' below will be changed to something like 'c:\folder\filename.doc' when viewed within the source application.
var fullPath = '{DOC:FILENAME}';
strPath = fullPath.substring(0,fullPath.lastIndexOf("\\"));

Open in new window

Question by:maiadog

Expert Comment

ID: 21818049
Is there a way you can encode the {DOC:FILENAME} before passing it to the javascript code?else you will have to write an encode function to change the fullPath string to proper format and then retrieve the substring you want.

Author Comment

ID: 21818204
No, the {DOC:FILENAME} is just a token that gets replaced by the application when the HTML is rendered, and the format the application uses is c:\folder\filename.doc. That is why I assign it to a variable so I can work with it.

Can you elaborate on the encode function, or point me to some sample code?  Thanks.
LVL 41

Expert Comment

ID: 21818613
Easiest is to use split() to separate the pieces, and join to put then back together again.  For example, something like:

var str="c:\\folder\\filename.doc'";
var temp = str.split( '\\' );
var name = temp.pop();
var path = temp.join( '\\' );

alert( 'Path: ' + path + '\nname: ' + name );
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.


Author Comment

ID: 21818780
Yes, but in your example, you show escaped backslashes , but as I indicated initially, the problem is that the software I am using returns a value that does not include escaped backslashes.
LVL 41

Expert Comment

ID: 21818797
That is true.  However, the way to represent the single backslash character,
when one is entering a string in JavaScript, requires that the backslash be
escaped using a backslash.

If you were to use the length() function to identify the length of the following
string, what do you think the result would be?

var str="c:\\folder\\filename.doc"

Here, try it yourself.    Go to this page:

and enter this "script", and press the "... click me" button
<script type="text/javascript">
var str="c:\\folder\\filename.doc";
document.write( 'str: "' + str + '"<br>' );
document.write( 'len: ' + str.length );

Open in new window


Author Comment

ID: 21818855
I do understand that, but I am not in control of how that value is returned to me from the source application, therefore perhaps I need a way to modify the string.

The only way I have of getting at that value is to use the token {DOC:FILENAME} which is returns a value that contains a non-escaped path when the page is rendered.
LVL 41

Expert Comment

ID: 21820728
This string does contain only 1 backslash  Not 2.

var str = 'c:\\filename.txt';

Try this:

  alert( 'str: "' + str + '"' )

and you will see a message like:

str: "c:\filename.txt"

So it does not contain an escaped path.  The escaped backslash
is only in the source code processed by the JavaScript interpreter.

Author Comment

ID: 21821853
I understand that your example is escaped and it is actually only one backslash.  But the application I am using *does not* render the page with an escaped backslash in the path.  I am trying to figure out how I can make it escaped as in your example.
LVL 41

Accepted Solution

HonorGod earned 63 total points
ID: 21824006
Well, it doesn't matter how the string value is obtained.
You can use the instructions shown above (and repeated here)
// Somehow, the variable "str" gets a fully qualified filename value,
// something like 'C:\folder\filename.doc'
// Note: this is simply a representation of the characters in the
//       string, not the syntax required to assign the value in
//       JavaScript
var temp = str.split( '\\' );    // Break the string into an array
var name = temp.pop();           // Remove the last item (filename.doc)
var path = temp.join( '\\' );    // Build the path using remnants
alert( 'Path: "' + path + '"\nname: "' + name + '"' );

Open in new window

LVL 54

Assisted Solution

b0lsc0tt earned 62 total points
ID: 21826166
I am not sure I understand what you need and how this works but if you want 2 backslashes as the result then use ...

var.replace(/\\/, "\\\\")

Of course you may need to use it in a different way and I can help with that but basically it will look for one backslash (escaped in the first argument above) and replace it wil 2 (both escaped in the second argument, the string, above).

Let me know if you have a question or need help using this.


Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In my daily work (mainly using, I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question