
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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?
In this article, we will learn to remove leading zeroes from numbers in JavaScript using Regex. When working with numeric strings in JavaScript, it's common to encounter unwanted leading zeroes. These zeroes can cause issues when parsing numbers, formatting data, or displaying user-friendly outputs.
Why Remove Leading Zeroes in JavaScript?
Leading zeroes can lead to inconsistencies in numeric string representation. For instance ?
- When parsing numbers, leading zeroes might result in incorrect interpretations.
- Displaying numbers with leading zeroes can confuse users or look unprofessional.
- Clean strings are essential for accurate mathematical operations and data storage.
By removing leading zeroes, you ensure uniformity and maintain clean data structures.
Using Regex with replace()
The replace() method is a simple and direct way to remove leading zeroes from strings. It searches for a specific pattern (in this case, leading zeroes) and replaces them with an empty string.
Following is the regex pattern we will use to remove leading zeroes from numbers ?
-
Regex Pattern: /^0+/
-
^: Asserts the start of the string.
- 0+: Matches one or more consecutive zeroes.
-
^: Asserts the start of the string.
- replace(/^0+/, ""): Replaces the matched leading zeroes with an empty string.
To remove leading zeros, use Regex in replace() method as in the below syntax ?
yourStringValue.replace(/\D|^0+/g, ""));
Example
Below is an example of removing leading zeroes from numbers using the replace() method ?
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, ""));
Output
5001000 100000005 5000001 6456
Using Regex with match()
The match() method provides an alternative by extracting the string part that doesn't include leading zeroes. Instead of replacing zeroes, it identifies the numeric part of the string.
Following is the regex pattern we will use to remove leading zeroes from numbers ?
-
Regex Pattern: /^0*(\d+)/
-
^: Matches the start of the string.
-
0*: Matches zero or more leading zeroes.
- (\d+): Captures one or more digits after the leading zeroes.
-
^: Matches the start of the string.
-
Using match(): Returns an array where:
-
result[0]: The full match.
- result[1]: The captured digits without leading zeroes.
-
result[0]: The full match.
Match leading zeroes and capture the rest ?
let result = value.match(/^0*(\d+)/);
Example
Below is an example of removing leading zeroes from numbers using the match() method?
var theValue1 = "5001000"; var theValue2 = "100000005"; var theValue3 = "05000001"; var theValue4 = "00000000006456"; function removeLeadingZeroes(value) { let result = value.match(/^0*(\d+)/); // Match leading zeroes and capture the rest return result ? result[1] : ""; // Return the captured group or an empty string } // Test Cases console.log(removeLeadingZeroes(theValue1)); console.log(removeLeadingZeroes(theValue2)); console.log(removeLeadingZeroes(theValue3)); console.log(removeLeadingZeroes(theValue4));
Output
5001000 100000005 5000001 6456
Comparison Table
Feature | replace() Method | match() Method |
Primary Action | Modifies the string by removing leading zeroes. | Extracts the numeric part without leading zeroes. |
Output | Modified string directly. | Captured group (digits without leading zeroes). |
Use Case | Simple string modifications. | Extracting numbers or parts of a string. |
Error Handling | Handles empty or invalid strings gracefully. | Requires validation for null matches. |