코드
function solution(files) {
const re = /^([a-zA-Z-\. ]+)([0-9]+)(.*)$/
let dict = []
files.forEach((entry, idx) => {
let [fn, head, num] = entry.match(re)
dict.push({fn, head: head.toLowerCase(), num: parseInt(num), idx})
})
return dict.sort((a, b) => {
if (a.head > b.head) return 1
if (a.head < b.head) return -1
if (a.num > b.num) return 1
if (a.num < b.num) return -1
return a.idx - b.idx
}).map(e => e.fn)
}
match에서 정규식
- 위에서 검증한 문자열은 'img12.png'
- 정규식이 위처럼 괄호로 묶여있으면 전체가 일치하는 결과를 먼저 반환하고(img12.png) 그 후 괄호로 묶인 친구들(img, 12, .png)를 뒤에 반환해요
- 예시

