在JavaScript中生成字符串/字符

时间:2017-08-23 00:26:15

标签: javascript

现在,我想将生成的输出设置为常量(前两个字母,后三个数字)。 我应该使用什么代码?感谢。

的Javascript

function makeid() {
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for (var i = 0; i < 5; i++)
    text += possible.charAt(Math.floor(Math.random() * possible.length));

  return text;
}
console.log(makeid());

6 个答案:

答案 0 :(得分:3)

您可以尝试将'可能'字符串分隔为possibleNums和possibleChars

var possibleNums = "0123456789";
var possibleAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

for (var i = 0; i < 2; i++) {
    text += possibleAlpha.charAt(Math.floor(Math.random() * possibleAlpha.length));
}

for (var i = 0; i < 3; i++) {
    text += possibleNums.charAt(Math.floor(Math.random() * possibleNums.length));
}

答案 1 :(得分:1)

试试这个:

function makeid() {
  var text = "";
  var possibleLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  var possibleNumbers = "0123456789";

  for (var i = 0; i < 2; i++)
    text += possibleLetters.charAt(Math.floor(Math.random() * possibleLetters.length));
  for (var i = 0; i < 3; i++)
    text += possibleNumbers.charAt(Math.floor(Math.random() * possibleNumbers.length));

  return text;
}

答案 2 :(得分:1)

您在ES6 -

中编码

您需要分隔数字和字符

但要生成随机的唯一ID ,您可以refer to here

const makeid = () => {
  let id = '';
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  const nums = '0123456789'
  for (let i = 0; i < 2; i++) {
    id += chars[Math.floor(Math.random() * chars.length) + 1]
  }
  for (let i = 0; i < 3; i++) {
    id += nums[Math.floor(Math.random() * nums.length) + 1]
  }
  return id
}

这里有更多功能性和可读性的代码:

const makeid = () => {
  let id = '';
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  const nums = '0123456789'
  id += generate(2, chars)
  id += generate(3, nums)
  return id
}
const generate = (number, string) => {
  let res = ''
  for (let i = 0; i < number; i++) {
    res += string[Math.floor(Math.random() * string.length) + 1]
  }
  return res
}
console.log(makeid())

答案 3 :(得分:0)

实际上,您不需要字符串“可能”,您可以使用ascii值。

下面是代码。

function makeid() {
  var text = "";
var x,y;
  for (var i = 0; i < 2; i++){
  x=(Math.floor(Math.random()*2))?( Math.floor(Math.random() * (26)) + 65):( Math.floor(Math.random() * (26)) + 97)
  text+=String.fromCharCode(x)
  }
  for (var i = 0; i < 3; i++){
 x=String.fromCharCode(Math.floor(Math.random() * (10)) + 48)
  text+=x
  } 
  return text;
}
console.log(makeid()); 

答案 4 :(得分:0)

&#13;
&#13;
var randomBetween = (min,max)=> Math.floor(max-Math.random()*(max-min));
var randomLetter = ()=> String.fromCharCode(randomBetween(65,90)+(Math.round(Math.random())?32:0));
var makeId = pattern=> (pattern||'xx000').replace(/./g, v=>isNaN(v)?randomLetter():randomBetween(0,9));

console.log(makeId());
&#13;
&#13;
&#13;

或者您也可以制作自己的模式,例如:

makeId('aaaaaa11111');   //returns: lUmKUe07356
makeId('a1a1a1a1a1a1a'); //returns: b2N5U8J2m7r2C

任何匹配的字母都将替换为随机字母,任意数字和随机数字。

答案 5 :(得分:0)

您可以尝试以下操作:

function getRandomId() {
    return String.fromCharCode((Math.random() < 0.5 ? 65 : 97) + Math.floor(Math.random() * 26)) +
        String.fromCharCode((Math.random() < 0.5 ? 65 : 97) + Math.floor(Math.random() * 26)) +
        Math.random().toString(10).substring(2, 5);
}

for (var i = 0; i < 5; i++) {
    console.log(getRandomId());
}

/* Sample Output:
wc257
bR495
Li690
cF973
kf790
*/