在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
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短