문제
문자열을 받아서 문자열에서 모든 특수 문자를 제거하고 결과 문자열에 알파벳과 숫자만 남기는 JavaScript 함수를 작성해야 합니다.
입력
const str = 'th@is Str!ing Contains 3% punctuations';
출력
const output = 'thisStringContains3punctuations';
모든 구두점과 공백을 제거했기 때문에
예시
다음은 코드입니다 -
const str = 'th@is Str!ing Contains 3% punctuations';
const removeSpecialChars = (str = '') => {
let res = '';
for(let i = 0; i < str.length; i++){
const el = str[i];
if(+el){
res += el;
}else if(el.toLowerCase() !== el.toUpperCase()){
res += el;
};
continue;
};
return res;
};
console.log(removeSpecialChars(str)); 출력
thisStringContains3punctuations