-1

Here is my code

"test.html".replace(/(?=\.[^.]+$)/g, Math.floor((Math.random() * 1000000) + 1))

How to convert test.html to random number.html like 38475.html?

Dreams
  • 7,748
  • 9
  • 41
  • 65

2 Answers2

2

if regex is not mandatory :), then try

 Math.floor((Math.random() * 1000000) + 1) + "." + "test.html".split( "." ).pop();

create a method

function randomizeFileName( fileName )
{
   return Math.floor((Math.random() * 1000000) + 1) + "." + fileName.split( "." ).pop();
}
gurvinder372
  • 64,240
  • 8
  • 67
  • 88
1

Why do you have to use regex here? you can just use the randaom number appended with file extension of actual file.

 var fileName="test.html";

 fileName= Math.floor((Math.random() * 1000000) + 1)+fileName.substr(fileName.lastIndexOf('.'))
Pavan Teja
  • 3,124
  • 1
  • 14
  • 22