js中隨機生成指定個數漢字


js中隨機生成指定個數漢字

點擊查看代碼
function randomlyGeneratedChineseCharacters(num) {
    let arr = []
    for (let i = 0; i < num; i++) {
        let str
		//漢字對應的unicode編碼為u4e00-u9fa5轉為10進制為19968-40869,先取隨機數,再轉為16進制
        str = '\\u' + (Math.floor(Math.random() * (40869 - 19968)) + 19968).toString(16)
		//在用正則操作數據后進行解碼。注意:unescape() 函數在 JavaScript 1.5 版中已棄用。請使用 decodeURI() 或 decodeURIComponent() 代替。
        str = unescape(str.replace(/\\u/g, "%u"));
        arr.push(str)
    }
    let chinese = arr.join("")
    return chinese
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM