Copy link to clipboard
Copied
Hi.
I have got the path for a file which contains slashes "/"
I need to use that path in my Javascript to load styles into my Application.
The syntax for that part requires colons to separate the folders, so I need to replace the slashes in the path.
I have tried:
mySlash = "\/";
myNewFilePath = myFilePath.replace (mySlash, ":");
but that doesn't replace.
I have also tried:
mySlash = "/";
but with no luck either
A similar line to replace "%20" with spaces works, so I know the replace function is working.
Does anyone know how i should construct this?
Cheers
Roy
1 Correct answer
try: mySlash = /\//;
Harbs
Copy link to clipboard
Copied
try: mySlash = /\//;
Harbs
Copy link to clipboard
Copied
Thanks Harbs.
I just needed to add g to make the search global.
Nice!
Cheers
Roy
Copy link to clipboard
Copied
On another note then, how can I make the following replace global?
myPath.replace("%20"," ");
Roy
Copy link to clipboard
Copied
I have made a while loop to do this, but would be nice to see to use the global search would've worked.
mySpace = "%20";
while (myFilePath.search("%20")!= -1)
{
myFilePath = myFilePath.replace (mySpace, " ");
}
Cheers
Roy
Copy link to clipboard
Copied
If I'm not mistaken, you only need to do this once:
myFilePath = myFilePath.replace (/%20/g, " ");
(the slash notation, rather than double quotes, instructs JS to use the GREP replace, which does allow the additional 'g'.)
(and I don't think the '%' is special inside GREPs, so it doesn't need a backslash escape.)
(.. which, thinking a bit further, makes the '%' character quite unique )
Copy link to clipboard
Copied
Thanks Guys.
I feel honoured having the answer come from 2 of the best!
Cheers
Roy

