JavaScript正则表达式从数字中删除前导零?
要删除前导零,请replace()按照以下语法在方法中使用Regex-
yourStringValue.replace(/\D|^0+/g, ""))
假设以下是我们具有数字值的变量-
var theValue1="5001000"; var theValue2="100000005"; var theValue3="05000001"; var theValue4="00000000006456";
示例
var theValue1="5001000"; var theValue2="100000005"; var theValue3="05000001"; var theValue4="00000000006456"; console.log(theValue1.replace(/\D|^0+/g, "")); console.log(theValue2.replace(/\D|^0+/g, "")); console.log(theValue3.replace(/\D|^0+/g, "")); console.log(theValue4.replace(/\D|^0+/g, ""));
要运行上述程序,您需要使用以下命令-
node fileName.js.
输出结果
在这里,我的文件名为demo101.js。这将产生以下输出-
PS C:\Users\Amit\JavaScript-code> node demo101.js 5001000 100000005 5000001 6456
热门推荐
10 广西考试祝福语结婚简短
11 猪年祝福语简短小孩
12 元旦祝福语送长辈简短
13 恭喜二宝祝福语简短
14 祝福语暖心话简短
15 国庆中秋祝福语简短兄弟
16 朋友订婚的祝福语简短
17 送弟弟中秋祝福语简短
18 爱生日祝福语简短独特