Reg exp to replace \ with /

F

Frank

Hi,
I'm not familiar with JavaScript but have to fix a problem.
Please see below part of the HTML code with contains my question:

The problem is that I have to display a path in the showIt function.
If I write the path (C:\TEMP\396.x00) directly in the document.write()
function is shows the path without the backslashed.

So I though I create a fixPath function that replaces the single backslash
to a forward backslash (better a forward slash than no slash; but I would
like to see a backslash if possible)

The fixPath however doesn't replace the backslash with a forward slash?
Why not?

FYI: I can't change the input string as it is generated by another program

Please help me.
Frank

....

<script language="JavaScript" type="text/javascript">
<!--


function fixPath( s )
{
return s.replace(/\\/g, "/");
}


function showIt()
{
document.write(fixPath("C:\TEMP\396.x00")+"<br>");
}
....
 
M

McKirahan

Frank said:
Hi,
I'm not familiar with JavaScript but have to fix a problem.
Please see below part of the HTML code with contains my question:

The problem is that I have to display a path in the showIt function.
If I write the path (C:\TEMP\396.x00) directly in the document.write()
function is shows the path without the backslashed.

So I though I create a fixPath function that replaces the single backslash
to a forward backslash (better a forward slash than no slash; but I would
like to see a backslash if possible)

The fixPath however doesn't replace the backslash with a forward slash?
Why not?

FYI: I can't change the input string as it is generated by another program

Please help me.
Frank

...

<script language="JavaScript" type="text/javascript">
<!--


function fixPath( s )
{
return s.replace(/\\/g, "/");
}


function showIt()
{
document.write(fixPath("C:\TEMP\396.x00")+"<br>");
}
...


Double up your backslashes:

"C:\\TEMP\\396.x00"
 
F

Frank

McKirahan said:
Double up your backslashes:

"C:\\TEMP\\396.x00"


Hi,
Thanks for the answer, but as I indicated I can't change the string
itself as it is generated by another program. I browsed this group a
bit and I think I must conclude that I can't fix my problem without
having a string that contains the double slash....Men, I'm glad my
daily job is not programming JavaScript ......;-)

Frank
 
L

Lasse Reichstein Nielsen

Thanks for the answer, but as I indicated I can't change the string
itself as it is generated by another program.

Yes, then you are lost. The other program generates Javascript with
less information than what you need, and there is no way around that ...
except, perhaps, if you are *really* desperate.

If you know that this string is contained inside a script on the page,
then you can (in some browsers) access the literal script content.
Say it's in the third script element on the page, then some browsers
(IE 5+, Mozilla, Opera 7) allows you to see the content of that
script element as:

document.getElementsByTagName("script")[2].text

If the string is somehow recognizable (starting with "C:" is suffient)
then you can find it inside the text:

var scriptText = document.getElementsByTagName("script")[2].text;
var start = scriptText.indexOf('"C:') + 1;
var end = scriptText.indexOf('"', start);
var literalText = scriptText.substring(start,end);


Example page:
---
<script type="text/javascript">
var stupidString = "C:\w\y\z"; // by someone else
</script>
<script type="text/javascript">
var scriptText = document.getElementsByTagName("script")[0].text;
var start = scriptText.indexOf('"C:') + 1;
var end = scriptText.indexOf('"', start);
var literalText = scriptText.substring(start,end);
alert(literalText);
</script>
---

On the other hand, if the script is included from an external file
(which it probably is, since you don't have control over it), then
I don't think there is anything to do. Hmm, and again. If you know
the user is using IE or Mozilla, then you might fetch the page again
using some sort of XMLHTTPRequest (but that's out of my scope, I
use neither IE nor Mozilla :).
I browsed this group a bit and I think I must conclude that I can't
fix my problem without having a string that contains the double
slash....

That would be the obvious solution, yes.
And just wait until you hit a directory starting with "x", "u" or "0"
(which start multi-character escape sequences that, if malformed
will be syntactic errors in your page, e.g.: "C:\x_drive\user" :)
Men, I'm glad my daily job is not programming JavaScript
......;-)

If it were, you'd most likely be in a position to force whoever
provided the string to stop being stupid :)

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top