[JS] 빈칸(스페이스) 제거

He SEO·2022년 2월 25일
0
post-thumbnail

1.정규 표현식 사용

const firstLine = " Call me Ishmael "; //양 끝과 중간에 빈칸 있음
console.log(firstLine.replace(/ /g, ""));

결과

CallmeIshmael

2.다른 방법

const firstLine = " Call me Ishmael "; 
console.log(firstLine);

결과

 Call me Ishmael 

2.1. trim()

trim()은 문자열 앞뒤의 공백만 메꿔줍니다.

console.log(firstLine.trim()); 

결과

Call me Ishmael

2.2. replace()

replace는 특정 문자를 특정 값으로 변환해 주는데, 하나의 문자만 변경됩니다.

console.log(firstLine.replace(" ", "")); 

결과

Call me Ishmael
profile
BACKEND 개발 기록 중. 감사합니다 😘

0개의 댓글