最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I replace the %20 characters in a path variable? Javascript - Stack Overflow

programmeradmin7浏览0评论

When using the folder.name or file.name Javascript classes, the returned values include the %20 characters in place of spaces in actual file or folder names.

For Example:

if (sFolder instanceof Folder) {
   folderArray.push(sFolder.name);
}

Returns:

Folder%20one, Folder%20two, Folder%20three

What I need is:

Folder one, Folder two, Folder three

The same thing is happening with files, if there are any spaces in the file name they are replaced with %20. How can I remove those characters if folder names have 1 or even multiple spaces?

When using the folder.name or file.name Javascript classes, the returned values include the %20 characters in place of spaces in actual file or folder names.

For Example:

if (sFolder instanceof Folder) {
   folderArray.push(sFolder.name);
}

Returns:

Folder%20one, Folder%20two, Folder%20three

What I need is:

Folder one, Folder two, Folder three

The same thing is happening with files, if there are any spaces in the file name they are replaced with %20. How can I remove those characters if folder names have 1 or even multiple spaces?

Share asked Oct 15, 2013 at 21:26 dimmechdimmech 8779 silver badges19 bronze badges 2
  • 2 folderArray.push(sFolder.name.replace('%20',' '))? – PlantTheIdea Commented Oct 15, 2013 at 21:28
  • 1 There is a replace function which should work developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Schleis Commented Oct 15, 2013 at 21:28
Add a ment  | 

3 Answers 3

Reset to default 4

use decodeURI()

decodeURI('Folder%20one, Folder%20two, Folder%20three');
// -> "Folder one, Folder two, Folder three"

%20 is the HTML encoded value for a space. URLs don't handle spaces, so they HTML/URL encode this value.

What you're looking for is decodeURIComponent.

You can see an example here

I found that the basic replace method only removed the first instance of the characters to be replaced. DecodeURI was a better answer however, I also found that you could use the following expression within the replace method and that you could use the method in succession for different character sets which was not in the documentation I read for that Method.

if (sFolder instanceof Folder) {
   folderArray.push(sFolder.name.replace (/%20/g,' ').replace ('.html', ''));
}
发布评论

评论列表(0)

  1. 暂无评论