var result = Math.random().toString(36).substring(2,6);

console.log(result);//包含0-9a-z

 

原理解析:

  1. Math.random()生成一个随机数;
  2. toString(36)转成字符串,遵循编码为36进制;
  3. substring(2,6)字符串截取,从索引为2(包含索引2)截取到索引为6(不包含索引6)的4个字符。(详见substring()方法);

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-14
  • 2022-12-23
  • 2021-09-01
  • 2021-11-18
  • 2022-02-25
  • 2021-08-17
猜你喜欢
  • 2021-11-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
相关资源
相似解决方案