JavaScript Regex 可从数字中删除前导零吗?
javascriptweb developmentobject oriented programming更新于 2025/11/11 14:04:17
要删除前导零,请在 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
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

