【问题标题】:Regular expression remove the special character from a file name for Windows正则表达式从 Windows 的文件名中删除特殊字符
【发布时间】:2014-08-08 17:35:07
【问题描述】:

来自http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx, 我们知道 Windows 保留了一些字符:

< (less than)
> (greater than)
: (colon)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)

我有一个包含这些特殊字符的文件名, 我想用“”替换那些, 像这样的东西 (string.replace(/\/g, '')
谢谢

【问题讨论】:

  • 这是一个具有挑战性的要求,因为 Windows 有奇怪的文件名规则。请参阅这篇 SO 文章:stackoverflow.com/questions/754307/…
  • 谢谢,我google的时候没找到。抱歉问了一个重复的问题

标签: javascript regex


【解决方案1】:

您可以将所有这些字符放入字符集中:

string.replace( /[<>:"\/\\|?*]+/g, '' );

【讨论】:

  • 我还会在正则表达式中添加八角/磅/数字符号 #,因为此字符会使文件无法被某些编码器链接
猜你喜欢
  • 2016-05-26
  • 2023-04-06
  • 2020-12-22
  • 2011-03-19
  • 1970-01-01
  • 1970-01-01
  • 2022-10-22
  • 1970-01-01
相关资源
最近更新 更多