let numbers = [1, 2, 3, 4, 5];
//forEach : 자신을 호출한 배열을 순회하면서 수행해야 할 처리를 콜백 함수로 전달받아 반복 호출한다.
//forEach의 콜백 함수는 요소, 인덱스, 배열 자체를 매개변수로 받는다.
numbers.forEach((num, idx, arr) => {
//'*'를 num번 출력
let result = "";
for(let i = 0; i < num; i++){
result += '*';
}
console.log(`${result} 인덱스 : ${idx} 배열 : ${arr}`);
});
// * 인덱스 : 0 배열 : 1,2,3,4,5
// ** 인덱스 : 1 배열 : 1,2,3,4,5
// *** 인덱스 : 2 배열 : 1,2,3,4,5
// **** 인덱스 : 3 배열 : 1,2,3,4,5
// ***** 인덱스 : 4 배열 : 1,2,3,4,5
let str = "There is an apple"
// '/'과 '/'으로 시작과 끝을 구분
// 'apple'이 패턴
// 'i'는 플래그. 대소문자를 구분하지 않는다.
const regexp = /apple/i;
//패턴을 검색해 결과를 반환
console.log(regexp.test(str)); //true
let str = '0\n00\n000\n0000\n00000';
// 0이 최소 2번, 최대 3번 반복되는 문자열을 찾는다.
// 콤마 뒤에 띄어쓰기를 하면 안된다.
const regexp = /0{2,3}/gm
console.log(str.match(regexp)); //[ '00', '000', '000', '000', '00' ]
let str = '0\n00\n000\n0000\n00000';
// 0이 다섯번 반복되는 문자열
const regexp = /0{5}/gm
console.log(str.match(regexp)); //[ '00000' ]
let str = '0\n00\n000\n0000\n00000';
//최소 3번 반복
const regexp = /0{3,}/gm
console.log(str.match(regexp)); //[ '000', '0000', '00000' ]
출처
이웅모, 모던 자바스크립트 Deep Dive(2021)