JavaScript中的许可keygen

发布于 2025-02-12 12:13:53 字数 928 浏览 1 评论 0原文

因此,我在这里看到了另一个解决方案,当时我遇到了一个问题。虽然问题并没有完全解决。

这是我在这里发现的代码。

function makeid(length) {
  var result = "";
  var characters = '0123456789';
  for (var i = 0; i < length; i++) {
    result += characters[Math.floor(Math.random() * characters.length)];
  }
  result = result.match(/\d{1,4}/g).join("-");
  return result;
}

console.log(makeid(16));

这是我现在拥有的当前代码。

function makeid(length) {
  var result = "";
  var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  for (var i = 0; i < length; i++) {
    result += characters[Math.floor(Math.random() * characters.length)];
  }
  result = result.match(/\d{1,4}/g).join("-");
  return result;
}

console.log(makeid(16));

我不想要输出。我希望输出在字母和数字中都可以使用,我不确定为什么它不会同时做!

So I saw another solution on here with a problem I was having. Though the problem isn't completely solved for me.

This is the code that I found on here.

function makeid(length) {
  var result = "";
  var characters = '0123456789';
  for (var i = 0; i < length; i++) {
    result += characters[Math.floor(Math.random() * characters.length)];
  }
  result = result.match(/\d{1,4}/g).join("-");
  return result;
}

console.log(makeid(16));

And this is the current code I have now.

function makeid(length) {
  var result = "";
  var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  for (var i = 0; i < length; i++) {
    result += characters[Math.floor(Math.random() * characters.length)];
  }
  result = result.match(/\d{1,4}/g).join("-");
  return result;
}

console.log(makeid(16));

The output is not want I want. I want the output to be in both Letters and Numbers and I'm not sure why it won't do both!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤单情人 2025-02-19 12:13:53

这是因为您的match()仅在寻找数字(\ d),而不是任何字符():

function makeid(length) {
  var result = "";
  var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  for (var i = 0; i < length; i++) {
    result += characters[Math.floor(Math.random() * characters.length)];

  }
  result = result.match(/.{1,4}/g).join("-");
  return result;
}

console.log(makeid(15));

您可以通过使用操作员来摆脱慢匹配一起:

function makeid(length) {
  var result = "";
  var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  for (var i = 0; i < length; i++) {
    result += characters[Math.floor(Math.random() * characters.length)];
    if (i+1 < length && !((i+1) % 4))
      result += "-";
  }
  return result;
}

console.log(makeid(16));

It's because your match() is only looking for digits ( \d ) and not any character ( . ):

function makeid(length) {
  var result = "";
  var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  for (var i = 0; i < length; i++) {
    result += characters[Math.floor(Math.random() * characters.length)];

  }
  result = result.match(/.{1,4}/g).join("-");
  return result;
}

console.log(makeid(15));

And you can get rid of slow match all together, by using % operator:

function makeid(length) {
  var result = "";
  var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  for (var i = 0; i < length; i++) {
    result += characters[Math.floor(Math.random() * characters.length)];
    if (i+1 < length && !((i+1) % 4))
      result += "-";
  }
  return result;
}

console.log(makeid(16));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文