Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

[JS] Replace a slash in a file path

Participant ,
Feb 23, 2010 Feb 23, 2010

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

TOPICS
Scripting
2.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Feb 23, 2010 Feb 23, 2010

try: mySlash = /\//;

Harbs

Translate
LEGEND ,
Feb 23, 2010 Feb 23, 2010

try: mySlash = /\//;

Harbs

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 23, 2010 Feb 23, 2010

Thanks Harbs.

I just needed to add g to make the search global.

Nice!

Cheers


Roy

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 23, 2010 Feb 23, 2010

On another note then, how can I make the following replace global?

myPath.replace("%20"," ");

Roy

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 23, 2010 Feb 23, 2010

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 23, 2010 Feb 23, 2010

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 )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 23, 2010 Feb 23, 2010
LATEST

Thanks Guys.

I feel honoured having the answer come from 2 of the best!

Cheers

Roy

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines