問(wèn)題
我們需要編寫一個(gè) JavaScript 函數(shù),它將字符串 str 作為第一個(gè)也是唯一的參數(shù)。
字符串 str 可以包含三種類型字符數(shù) –
英文字母:(A-Z)、(a-z)
數(shù)字:0-9
特殊字符 – 所有其他剩余字符
我們的函數(shù)應(yīng)該迭代該字符串并構(gòu)造一個(gè)包含以下內(nèi)容的數(shù)組:正好三個(gè)元素,第一個(gè)包含字符串中存在的所有字母,第二個(gè)包含數(shù)字,第三個(gè)特殊字符保持字符的相對(duì)順序。我們最終應(yīng)該返回這個(gè)數(shù)組。
例如,如果函數(shù)的輸入是
輸入
const str = 'thi!1s is S@me23';
登錄后復(fù)制
輸出
const output = [ 'thisisSme', '123', '! @' ];
登錄后復(fù)制
示例
以下是代碼 –
?實(shí)時(shí)演示
const str = 'thi!1s is S@me23'; const regroupString = (str = '') => { const res = ['', '', '']; const alpha = 'abcdefghijklmnopqrstuvwxyz'; const numerals = '0123456789'; for(let i = 0; i < str.length; i++){ const el = str[i]; if(alpha.includes(el) || alpha.includes(el.toLowerCase())){ res[0] += el; continue; }; if(numerals.includes(el)){ res[1] += el; continue; }; res[2] += el; }; return res; }; console.log(regroupString(str));
登錄后復(fù)制
輸出
[ 'thisisSme', '123', '! @' ]
登錄后復(fù)制
以上就是在 JavaScript 中重新組合字符串的字符的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!