split into course names

// 如:'國語文1011010101(25)' -> '國語文'
function courseName(courseCode){
    
    // 1. 科目名稱 (非數字、空白、逗號)
    // 2. 科目代碼 (數字開頭、英數字後續)
    // 3. 學生成績 (一到二位數)
    //                               ╭── 1 ──╮  ╭─── 2 ───╮    ╭─ 3 ─╮
    const found = courseCode.match(/([^\d\s,]+)(\d[0-9A-Z]+)\((\d{1,2})\)/);
    
    if(!found) return null;
    return found[1];            // capture group 1
}

// 如:'國語文1011010101(25),英語文1011010102(14)' -> ['國語文', '英語文']
function splitIntoCourseNames(courseCodes){
    return courseCodes
        .replaceWhitespaces('')    // 去空白   (string)
        .split(',')                // 科目名稱+科目代碼+學生成績 (array)
        .map(s => courseName(s));  // 科目名稱 (array)
}

let courses = splitIntoCourseNames('國語文1011010101(25),英語文1011010102(14),數學A1011010204(15),歷史1011010309(32),藝術生活101101050W(15),體育1011010815(36),國學常識1013010101(12),探究與實作:地理與人文社會科學研究101301030H(12)');
// [
//   '國語文',
//   '英語文',
//   '數學A',
//   '歷史',
//   '藝術生活',
//   '體育',
//   '國學常識',
//   '探究與實作:地理與人文社會科學研究'
// ]

Last updated