スニぺったん

無料のコードスニペットを掲載しています。言語ごとにコードスニペットを検索し、利用することが可能です。コードのライセンスはトップページをご覧ください。

  • JavaScript
  • ランダムなIDの文字列を生成する関数 (genRandomID)

ランダムなIDの文字列を生成する関数 (genRandomID)

総合評価: - 作成日: 2025-11-13

コメント:
Braveブラウザで動作確認済み。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>genRandomID</title>
    <script>
        /**
         * ランダムなID文字列を生成する。
         *
         * len ... 生成する文字列の長さ
         * smallAlpha ... 生成に小文字の英字を使うならtrue
         * bigAlpha ... 生成に大文字の英字を使うならtrue
         * number ... 生成に数字を使うならtrue
         * optional ... 追加で生成に使う文字列
         *
         * 返り値 ... 文字列
         */
        function genRandomID (len=16, {
            smallAlpha=true,
            bigAlpha=true,
            number=true,
            optional=null,
        }={}) {
            let code = ''
            let s = ''

            if (smallAlpha) {
                code += 'abcdefghijklmnopqrstuvwxyz'
            }
            if (bigAlpha) {
                code += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
            }
            if (number) {
                code += '0123456789'
            }
            if (optional) {
                code += optional
            }
            if (!code.length) {
                return s
            }

            for (let i = 0; i < len; i++) {
                const i = parseInt(Math.floor(Math.random()*code.length))
                s += code[i]
            }

            return s
        }
    
        document.addEventListener('DOMContentLoaded', () => {
            let s 

            s = genRandomID()
            console.log(s)
            console.assert(s.length === 16)
            console.assert(s.match(/[0-9a-zA-Z]+/) != null)

            s = genRandomID(16, { number: false })
            console.log(s)
            console.assert(s.length === 16)
            console.assert(s.match(/[a-zA-Z]+/) != null)

            s = genRandomID(16, { smallAlpha: false })
            console.log(s)
            console.assert(s.length === 16)
            console.assert(s.match(/[0-9A-Z]+/) != null)

            s = genRandomID(16, { bigAlpha: false })
            console.log(s)
            console.assert(s.length === 16)
            console.assert(s.match(/[0-9a-z]+/) != null)

            s = genRandomID(8)
            console.log(s)
            console.assert(s.length === 8)
            console.assert(s.match(/[0-9a-zA-Z]+/) != null)

            s = genRandomID(32)
            console.log(s)
            console.assert(s.length === 32)
            console.assert(s.match(/[0-9a-zA-Z]+/) != null)

            s = genRandomID(32, { optional: '_+-' })
            console.log(s)
            console.assert(s.length === 32)
            console.assert(s.match(/[0-9a-zA-Z\_\+\-]+/) != null)

            s = genRandomID(16, {
                smallAlpha: false,
                bigAlpha: false,
                number: false,
            })
            console.log(s)
            console.assert(s.length === 0)
        })      
    </script>
</head>
<body>
</body>
</html>