在JavaScript中以空格开头替换零
我们需要编写一个JavaScript函数,该函数接受一个表示数字的字符串。
用数字中的空格替换前导零。确保保留前面的空格。
例如:如果字符串值定义为-
"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
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便