js随机生成字符串

需求:有时候我们需要数字字母下划线结合而生成的字符串(注意:临时邮箱前缀大多这样生成)
例子:wvwn_tk59 这样的形式

代码如下:

import execjs
js_function_str = '''function randomString(len, charSet) {
             charSet = charSet || 'abcdefghijklmnopqrstuvwxyz0123456789_';
             var randomString = '';
             for (var i = 0; i < len; i++) {
             var randomPoz = Math.floor(Math.random() * charSet.length);
             randomString += charSet.substring(randomPoz, randomPoz + 1);
            }
             return randomString;
          }'''
js_return = execjs.compile(js_function_str)
js_number = js_return.call('randomString',9)
9为参数,在这里我们生成的是字符串长度为9,可变为8,7,6.....自己想要的字符串长度
print(js_number)

你可能感兴趣的:(js随机生成字符串)