View Full Version : Replace "\" with "/" in a string
Graeme Hackston
10-06-2002, 06:02 PM
I've been playing with this example:
http://www.devguru.com/Technologies/ecmascript/quickref/string_replace.html
The closest I've gotten so far is to include 2 back slashes in the string
<script>
myString = new String("Go to \\ DevGuru \\ today!")
rExp = /\\/;
results = myString.replace(rExp,"/")
document.write(results)
</script>
How can I replace single instances of back slashes with forward slashes?
<edit>The 2nd set of back slashes in the string does'nt work at all but I put them in there because I need to replace more than 1 instance in the string</edit>
Graeme Hackston
10-06-2002, 08:56 PM
I figured out a work around using .split("\\") then putting the pieces back together with .concat("/") but I'm still wondering if it's possible with regular expressions?
mordred
10-06-2002, 10:53 PM
Strange, all I did was adding the global modifier and it worked just fine for me in Moz1.0 and IE5.5 on win32...
myString = new String("Go to \\ DevGuru \\ today!")
rExp = /\\/g;
results = myString.replace(rExp,"/")
document.write(results);
If you still have problems getting this snippet to work, it would be interesting to know the browser you test with, because there *can* be differences in handling of regular expressions.
Graeme Hackston
10-06-2002, 11:25 PM
Thanks Mordred, that works as intended in IE6. I'm pretty sure I tried adding the global modifier but I probably had something else wrong at the same time.
I know it's probably not possible if the string is:
("Go to \ DevGuru \ today!")
because the single back slashes are read as "escape next character".
I have it working the way I said in my 2nd post but really only in a controlled environment.
If I wanted to convert a file name from:
C:\My Documents\WebSites\photo\images\weddings\p_Janet&Gary.jpg
to:
images/weddings/p_Janet&Gary.jpg
I can use this:
<html>
<head>
<title></title>
<script>
function RelativePath() {
raw = document.form1.browse.value;
str = raw.split("\\")
folder1 = str[4]
folder2 = str[5]
file = str[6]
output = ((((folder1.concat("/")).concat(folder2)).concat("/")).concat(file));
alert(output)
}
</script>
</head>
<body>
<form name=form1>
<input type=file name="browse" onchange="RelativePath()">
</form>
</body>
</html>
But this only works if the file is at a specific folder depth in the site. What I'm trying to figure out now is if there is a way to count the occurrences of "\" in the string. If I can get that I should be able to piece together a relative path regardless of the folder depth.
mordred
10-07-2002, 12:40 AM
It's not quite clear to me how you want to conduct a possibly relative path to the page in question by counting the backslashes of the file location string. Thinking about that, I think that you can only accomplish this if you know the location of the file itself beforehand (hmh, and while writing this, I remembered that that should be exactly the information in location.href - silly me :) . But maybe not completely, I often enough open a site from my local server and not from the filesystem).
Anyway, the number of backslashes should be the length of the array you get from splitting the string - 1.
Like
str = raw.split("\\");
alert(str.length);
Is that what you wanted?
Graeme Hackston
10-07-2002, 01:18 AM
Cool thanks Mordred. What I'm doing is making a form to simplify page production. I know that the site is located in "photo" on my hard drive. From there I can find out how deep the file is in the web by using .length and produce a relative path src attribute for the page that will contain the image. Once an image is saved somewhere in the photo folder I "should" be able to get the correct relative path by just browsing to it.
Graeme Hackston
10-07-2002, 02:38 AM
Thanks again Mordred. This is kind of lengthy but it seems to work. It goes to 5 folders deep in the site.
"photo" is the folder that my site is located in on my hard drive. If anyone wants to test it (or god forbid has a use for it :-) they can do so by replacing photo with their folder name.
<html>
<head>
<title></title>
<script>
function RelativePath() {
raw = document.form1.browse.value;
site = raw.split("photo\\")
str = site[1].split("\\")
if (str.length == 1) {
src = str;
}
if (str.length == 2) {
src = ((str[0].concat("/")).concat(str[1]));
}
if (str.length == 3) {
src = ((((str[0].concat("/")).concat(str[1])).concat("/")).concat(str[2]));
}
if (str.length == 4) {
src = (((((str[0].concat("/")).concat(str[1])).concat("/")).concat(str[2])).concat("/").concat(str[3]));
}
if (str.length == 5) {
src = ((((((str[0].concat("/")).concat(str[1])).concat("/")).concat(str[2])).concat("/").concat(str[3])).concat("/").concat(str[4]));
}
if (str.length == 6) {
src = (((((((str[0].concat("/")).concat(str[1])).concat("/")).concat(str[2])).concat("/").concat(str[3])).concat("/").concat(str[4])).concat("/").concat(str[5]));
}
alert(src)
}
</script>
</head>
<body>
<form name=form1>
<input type=file name="browse" onchange="RelativePath()">
</form>
</body>
</html>
joh6nn
10-07-2002, 03:31 AM
i think you've confused two different methods, Graeme. concat() is the same thing as + . i'm pretty sure what you're looking for is Array.join(string), which joins all the indices in an array, with a string argument. by default, it's a comma, but you can substitute whatever you like, for instance, a /
also, i'm not sure why you need to know the number of \'s present in a string; you don't seem to do anything with that information, or ever even collect it.
as far as using String.replace() is concerned, is you're problems with it, have to do with the fact that you were hardcoding your testing variables. when you enter a \ into an input box, then the \'s are already escaped; the special characters are going to be inputted directly, not as escaped characters. when you hard code the variable, you have to escape the special characters. don't know how clear that is, but i hope you see what i'm getting at.
so, your original String.replace("\\","/") , should work just fine.
also, for future reference, when you have a situation where you have to do bunches of addition like that, you can use something like this:
for (var i = 0; i < Array.length: i++) {
String += Array[i] + "/";
}
Graeme Hackston
10-07-2002, 11:03 PM
Thanks for the tutorial John. Very enlightening. Your right it does work fine from an input box.
If I understand you correctly, the reason I wanted to know how many "\" were in the string is because of the way I had it written in my 3rd post. If the path to the file was another folder above what the function was written for I would get output like this:
images/p_Janet&Gary.jpgundefined
I understand what you mean by it being hard coded but I'm not clear on how to use the code below in addition. Could you give me a little mock up of what it would look like?
for (var i = 0; i < Array.length: i++) {
String += Array[i] + "/";
}
Here's a much smaller version of the function.
function RelativePath() {
raw = document.form1.browse.value;
site = raw.split("photo\\")
rExp = /\\/g;
src = site[1].replace(rExp,"/")
alert(src)
}
joh6nn
10-08-2002, 03:01 AM
it's an either-or situation. you use one piece of code, or another. you could use
this:
if (str.length == 2) {
src = ((str[0].concat("/")).concat(str[1]));
}
if (str.length == 3) {
src = ((((str[0].concat("/")).concat(str[1])).concat("/")).concat(str[2]));
}
if (str.length == 4) { ...
or this:
for (var i = 0; i < Array.length: i++) {
String += Array[i] + "/";
}
or this:
src = site[1].replace(rExp,"/")
The last bit of what i had been saying, is that when you come across a situation like the first piece of code, and you don't already have a method that you can use, like the third piece of code, then instead of going step-by-step like the first piece of code does, use a for-loop like the second piece of code. get it?
whammy
10-08-2002, 03:15 AM
That sounds exactly like my logic which I don't know how to explain, including regarding functions and regular expressions in ASP... :)
I guess that kind of knowledge only comes from playing around with them. (And I'm always learning!)...
It just seems that sometimes a loop is the best solution, but more often than not a regular expression is what you want to use.
Graeme Hackston
10-08-2002, 03:19 AM
I thought by looking at it in action I could figure out how it works but I can see now that it’s plug and play. So I guess I should have asked if you could splain me how it works?
joh6nn
10-08-2002, 03:39 AM
what is it that you don't understand?
Graeme Hackston
10-08-2002, 04:03 AM
I get this part other than how the program knows what Array I'm talking about.
for (var i = 0; i < Array.length: i++) {
I don't understand what the += part of String += is doing.
I hope that makes sense.
joh6nn
10-08-2002, 04:15 AM
this is just example code, as an explanation of how to do it:
for (var i = 0; i < Array.length: i++) {
String += Array[i] + "/";
}
it wouldn't actually run.
this1 += that1 is the same as saying this1 = this1 + that1;
whammy
10-08-2002, 04:20 AM
yeah... in ASP:
myString = myString & "whatever" (concatenation)
is the same as javascript's
myString += "whatever" // IF you are working with a string. Otherwise, in javascript and many other languages, those are mathematical operators, i.e.:
myString += myString
myNum *= myNum
which is the same as
myNum = myNum * myNum
also:
-=
/=
etc...
Graeme Hackston
10-08-2002, 04:23 AM
OIC thanks John. So how would I connect the str Array in this example to the loop?
str = site[1].split("\\")
joh6nn
10-08-2002, 04:58 AM
...
i think i lost you in there somewhere. i was suggesting that you use String.replace(), not that you use the loop. if the replace() method didn't exist, then, and only then, would you use the loop. so, the code you want is this:
function RelativePath() {
raw = document.form1.browse.value;
site = raw.split("photo\\")
src = site[1].replace("\\","/")
alert(src)
}
Graeme Hackston
10-08-2002, 10:47 PM
Thanks for all the help guys
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.