有没有办法将任意字符串转换为 Javascript 中的有效文件名?
结果应尽可能接近原始字符串,以便于人类阅读(因此 slugify 不是一个选项)。这意味着它只需要替换 are not supported by an OS 中的字符.
例如:
'Article: "Un éléphant à l\'orée du bois/An elephant at the edge of the woods".txt'
→ 'Article Un éléphant à l\'orée du bois An elephant at the edge of the woods .txt'
我认为这是一个常见问题,但我还没有找到任何解决方案。我希望你能帮助我!
最佳答案
使用字符串替换功能
var str = 'Article: "Un éléphant à l'orée du bois/An elephant at the edge of the woods".txt';
var out = (str.replace(/[ &\/\\#,+()$~%.'":*?<>{}]/g, ""));
或者期望数字和字母
var out=(str.replace(/[^a-zA-Z0-9]/g, ''));
关于javascript - 如何从 JavaScript 中的任意字符串创建有效的文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35511331/