- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
- Related Articles
- Explain how to remove Leading Zeroes from a String in Java
- Remove Leading Zeroes from a String in Java using regular expressions
- Display numbers with leading zeroes in Java
- How to remove leading zeros from a number in JavaScript?
- Remove leading zeros in array - JavaScript
- Remove leading zeros in a JavaScript array?
- Remove leading zeros in array in JavaScript
- Remove Leading Zeros From String in Java
- How to preserve leading 0 with JavaScript numbers?
- Python program to remove leading zeros from an IP address
- Remove Leading Zeros from a String in C#
- Remove Leading Zeros from an Array using C++
- Java Program to remove leading and trailing whitespace from a string
- Java Program to Remove leading zeros
- Python program to remove leading 0's from an IP address

Advertisements