Replacing a backslash with a forward slash in Javascript

K

Kberg

In case anyone is interested, this is how I solved the problem of
replacing a back slash with a forward slash (as in being able to access
a document on a file share on an intranet via a browser)

if you cut and paste a path from windows explorer
\\myshare\mydir\mydoc.txt into a form textbox, you can use the
following code to transform it into something usable by a browser:

mytxt = document.myForm.myTextBox.value;
mytxt = mytxt.replace(/\134/g,"/");

\134 is the octal representation of a backslash as a regular
expression. the g is required to replace all instances of the
backslash.
 
M

Martin Honnen

Kberg said:
mytxt = document.myForm.myTextBox.value;
mytxt = mytxt.replace(/\134/g,"/");

\134 is the octal representation of a backslash as a regular
expression. the g is required to replace all instances of the
backslash.

Why not simply
mytxt = mytxt.replace(/\\/g,"/");
? I am not sure octal escape sequences in regular expression patterns
are standardized and support anywhere.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top