लीडिंग जीरो हटाने के लिए, 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