숫자를 나타내는 문자열을 받는 JavaScript 함수를 작성해야 합니다. 선행 0을 숫자의 공백으로 바꿉니다. 앞의 빈칸이 그대로 유지되는지 확인해야 합니다.
예를 들어,
문자열 값이 -
로 정의된 경우" 004590808"
그런 다음 출력은 다음과 같아야 합니다. -
" 4590808"
예시
다음은 코드입니다 -
const str = ' 004590808';
const replaceWithSpace = str => {
let replaced = '';
const regex = new RegExp(/^\s*0+/);
replaced = str.replace(regex, el => {
const { length } = el;
return ' '.repeat(length);
});
return replaced;
};
console.log(replaceWithSpace(str)); 출력
이것은 콘솔에서 다음과 같은 출력을 생성합니다 -
4590808