Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 438
  • Last Modified:

Problem with escape characters when splitting string into path/filename

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

2 Solutions
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.
maiadogAuthor Commented:
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.
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 );
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

maiadogAuthor Commented:
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.
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

maiadogAuthor Commented:
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.
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.
maiadogAuthor Commented:
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.
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

b0lsc0ttIT ManagerCommented:
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now