![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript Regex to remove leading zeroes from numbers?
To remove leading zeros, use Regex in replace() method as in the below syntax −
yourStringValue.replace(/\D|^0+/g, ""))
Let’s say the following are our variables with number values −
var theValue1="5001000"; var theValue2="100000005"; var theValue3="05000001"; var theValue4="00000000006456";
Example
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, ""));
To run the above program, you need to use the following command −
node fileName.js.
Output
Here, my file name is demo101.js. This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo101.js 5001000 100000005 5000001 6456
Advertisements