JavaScript Remove Zeros from a String

This article will remove all zeros from a javascript string using different methods and example illustrations. While developing in any language, removing zeros from a string is common before processing it. Here, we will discuss removing leading zeros, trailing zeros, and other zeros within the string.

We will be using replace() and replaceAll() methods of javaScript.

Javascript’s¬†replace()¬†method finds a pattern and replaces some or all of its occurrences with a replacement(character/string). The pattern can be a character, or a string, or a regular expression.

Javascript’s replaceAll() method will replace all the pattern matches within a string with a replacement. The first argument is a pattern that can be a string or a regular expression. This first argument is to be searched in string and replaced with a replacement. The second argument is a replacement which can be a string or a function.

Table of Contents:-

Advertisements

Javascript String Remove All Leading Zeros

Removing leading zeros means removing the zeros from the beginning of the string.

Remove all leading zeros from the string“000javasc0ript000”

Code:-

let dummyString = "000javasc0ript000";
let finalString = dummyString.replace(/^0+/, '');
console.log("Original String: " + dummyString);
console.log("Final String: " + finalString);

Output:-

Original String: 000javasc0ript000
Final String: javasc0ript000

Explanation

Here, replace() method is used to replace the zero with ”. The regular expression used is: /^0+/

  • Regular Expression Reference:
  • / and / specify the beginning and end of the string.
  • ^ specifies the beginning of the string.
  • + specifies one or more characters.
  • 0 is the digit zero.

Javascript String Remove All Trailing Zeros

Removing trailing zeros means removing the zeros from the end of the string.

Remove all trailing zeros from the string“000javasc0ript000”

Code:-

let dummyString = "000javasc0ript000";
let finalString = dummyString.replace(/0+$/, '');
console.log("Original String: " + dummyString);
console.log("Final String: " + finalString);

Output:-

Original String: 000javasc0ript000
Final String: 000javasc0ript

Explanation

Here, replace() method is used to substitute zero with ”. The regular expression used is: /0+$/

  • Regular Expression Reference:
  • / and / specify the beginning and end of the string.
  • + specifies one or more characters.
  • $ specifies the end of the string.
  • 0 is the digit zero.

Javascript String Remove Leading and Trailing Zeros

Removing leading and trailing zeros means removing zeros from the starting and at the end of a string.

Remove all leading and trailing zeros from the string“000javasc0ript000”

Code:-

let dummyString = "000javasc0ript000";
let finalString = dummyString.replace(/^0+/,'').replace(/0+$/,'');
console.log("Original String: " + dummyString);
console.log("Final String: " + finalString);

Output:-

Original String: 000javasc0ript000
Final String: javasc0ript

Explanation

Here, replace() method is used twice. First replace() method will remove all the leading zeros. Second replace() method will remove all the trailing zeros.

Javascript String Remove All Zeros from a String

Remove all zeros from the string“000javasc0ript000”

Code:-

let dummyString = "000javasc0ript000";
let finalString = dummyString.replaceAll('0', '');
console.log("Original String: " + dummyString);
console.log("Final String: " + finalString);

Output:-

Original String: 000javasc0ript000
Final String: javascript

Explanation

Here, replaceAll() method is used to replace the character (‘0’) with nothing (”).

I hope this article helped you remove leading and trailing zeros from a javaScript string. Good Luck !!!

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top