문제: https://school.programmers.co.kr/learn/courses/30/lessons/72410
문제에 나온 조건을 그대로 코드로 옮기는 방식으로 구현했다.
function solution(new_id) {
let recommend_id = new_id.toLowerCase() // 1단계
.replace(/[^a-z\d-_.]+/g, "") // 2단계
.replace(/\.+/g, ".") // 3단계
.replace(/^\.|\.$/, ""); // 4단계
if (!recommend_id.length) {
recommend_id = "a"; // 5단계
}
recommend_id = recommend_id.slice(0, 15).replace(/\.$/, ""); // 6단계
if (recommend_id.length <= 2) {
const lastString = recommend_id.slice(-1);
while(recommend_id.length < 3) {
recommend_id += lastString; // 7단계
}
}
return recommend_id;
}