본문 바로가기
반응형

개발/javascript15

[javascript] 자주 사용하는 정규 표현식 (Regular Expression) 정리 정규표현식 패턴 /패턴/ 대표적인 패턴 의미 패턴 의미 x 문자 x xyz 문자 xyz [xyz] x,y,z 중 하나의 문자 [a-z] a~z중 하나의 문자 [^xyz] x,y,z 가 아닌 하나의 문자 [^a-z] a~z가 아닌 하나의 문자 abc|xyz 문자열 abc 또는 xyz {숫자} 반복 횟수 ^x 시작문자 x x$ 종료문자 x . 하나의 문자 x* 0개이상 계속되는 x \ 다음에 오는 문자를 이스케이프 처리 \d 숫자 0~9 \D 숫자가 아닌 문자 = [^0-9] \w 영문, 숫자, 언더바 = [A-Za-z0-9_] \s 공백문자(스페이스, 탭, 줄바꿈 등) \S 공백문자 이외의 문자 = [^\s] \t 수평탭 \n 줄바꿈 코드 정규표현식 테스트 예제 //======================.. 2021. 1. 18.
[javascript] Async / Await 예제 Async / Await 예제 함수앞에 async를 입력하고 리턴 앞에 await를 입력 async function getTodos() { const response = await fetch("https://jsonplaceholder.typicode.com/todos"); const json = await response.json(); console.log(json); } getTodos(); 아래와 같이 입력하여 바로 호출할 수도.. ( async () => { const response = await fetch("https://jsonplaceholder.typicode.com/todos"); const json = await response.json(); console.log(json); })(); 2021. 1. 18.
[javascript] Promiss 예제 예제 let myPromise = new Promise( (resolve, reject) => { // async 코드 실행 //... //... // isTrue = true; // or isTrue = false; if (isTrue) { resolve("SUCCESS!!") } else { reject("FAILURE!!") } }); myPromise .then(success => { // ... }) .catch(failure => { // ... }); 2021. 1. 18.
[javascript] fetch 예제 fetch 예제 문법 fetch( url, { options } ) 예제1 - 기본 사용 fetch("https://jsonplaceholder.typicode.com/todos") .then(response => response.json()) .then(json => console.log(json)) 예제2 - 기본사용의 출력 내용 fetch("https://jsonplaceholder.typicode.com/todos/1") .then(response => response.json()) .then(json => console.log(json)) // console output { id: 1, title: "delectus aut autem", complete: true, userId:1 } 예제3 -.. 2021. 1. 18.
[javascript] Spread Operator (스프레드 연산자) 예제 사용법 myFunction(...iterableObj); 함수를 호출할때 인자값으로 사용하는 예제 function sum(x, y, z) { return x + y + z; } const numbers = [1,2,3]; console.log( sum(...numbers) ); // 6 const numbers2 = [1,2,3,4]; console.log( sum(...numbers2) ); // 6 // sum함수에서 인자값 3개만 받아서 연산하기 때문에 결과는 6 스프레드 연산자를 배열에 넣는 예제 const arr1 = [1,2]; const arr2 = [3,4]; const arr3 = [...arr1, 5, ...arr2]; console.log( arr3 ); // [1,2,5,3,4] 문.. 2021. 1. 18.
[javascript] Closures 예제 Closures 예제 함수와 함수 밖의 변수 접근 let outsideVariable = true; const myFunction = () => { // 함수 밖의 모든 데이터 접근 가능 // outsideVariable 변수 접근 가능 } 함수와 함수 밖의 글로벌 변수 접근 예제 const a = 3; //===Global Scope //=== function add() { //--- Function Scope //=== console.log( a + a ); // 6 //--- //=== } //--- //=== //=== add(); //=== 함수에서 함수 밖의 글로벌 변수값에 접근 let counter = 0; function add() { counter++; } add(); // counter.. 2021. 1. 18.
반응형