> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/val/prim/str/method/str.match/split-into-course-names.md).

# split into course names

{% tabs %}
{% tab title="💾 程式" %}

* [replit](https://replit.com/@pegasusroe/split-into-course-names#index.js) , [RegExr](https://regexr.com/6ov8d)
* require: [str.replaceWhitespaces()](/web/js/val/prim/str/method/str.replacewhitespaces.md)

```javascript
// 如：'國語文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',
//   '歷史',
//   '藝術生活',
//   '體育',
//   '國學常識',
//   '探究與實作：地理與人文社會科學研究'
// ]
```

{% endtab %}

{% tab title="⬇️ 應用" %}

* [不能補考名單](/web/appendix/gas/projects/no-make-up-exam.md)
  {% endtab %}

{% tab title="👥 相關" %}

* [str.match()](/web/js/val/prim/str/method/str.match.md)
  {% endtab %}
  {% endtabs %}
