How to replace full file name with random number?
这是我的代码
1 | "test.html".replace(/(?=\\.[^.]+$)/g, Math.floor((Math.random() * 1000000) + 1)) |
如何将test.html转换为随机数(例如38475.html)?
如果不是必需的正则表达式:),请尝试
1 | Math.floor((Math.random() * 1000000) + 1) +"." +"test.html".split("." ).pop(); |
创建方法
1 2 3 4 | function randomizeFileName( fileName ) { return Math.floor((Math.random() * 1000000) + 1) +"." + fileName.split("." ).pop(); } |
为什么在这里必须使用正则表达式?
您可以只使用带有实际文件扩展名的随机编号。
1 2 3 | var fileName="test.html"; fileName= Math.floor((Math.random() * 1000000) + 1)+fileName.substr(fileName.lastIndexOf('.')) |