Removing / trimming javascript / jquery leading zeros

Suggest a solution to remove or truncate leading zeros from a number (any string) using javascript, jquery.

+64
javascript jquery numbers zero
Nov 26 '11 at 5:15
source share
12 answers

You can use a regular expression that matches zeros at the beginning of a line:

s = s.replace(/^0+/, ''); 
+147
Nov 26 '11 at 5:18
source share
— -

I would use the Number () function:

 var str = "00001"; str = Number(str).toString(); 
 >> "1" 

Or I would multiply my string by 1

 var str = "00000000002346301625363"; str = (str * 1).toString(); 
 >> "2346301625363" 
+22
Jul 23 '14 at 18:47
source share

Since you said "any line", I assume this is also the line you want to process.

 "00012 34 0000432 0035" 

So regex is the way to go:

 var trimmed = s.replace(/\b0+/g, ""); 

And this will prevent the loss of the value "000000".

 var trimmed = s.replace(/\b(0(?!\b))+/g, "") 

Here you can see the working

+11
Nov 26 '11 at 5:44
source share

Maybe a little late, but I want to add my 2 cents.

if your ALWAYS string represents a number, with possible leading zeros, you can simply pass the string to a number using the "+" operator.

eg.

 x= "00005"; alert(typeof x); //"string" alert(x);// "00005" x = +x ; //or x= +"00005"; //do NOT confuse with x+=x, which will only concatenate the value alert(typeof x); //number , voila! alert(x); // 5 (as number) 

if your string does not represent a number and you only need to remove 0, use other solutions, but if you need them only as a number, this is the shortest way.

and FYI you can do the opposite, force the numbers to act like strings if you combine an empty string into them, for example:

 x = 5; alert(typeof x); //number x = x+""; alert(typeof x); //string 

hope this helps someone

+11
Nov 05 '13 at 0:58
source share

I got this solution for trimming leading zeros (a number or any string) in javascript:

 <script language="JavaScript" type="text/javascript"> <!-- function trimNumber(s) { while (s.substr(0,1) == '0' && s.length>1) { s = s.substr(1,9999); } return s; } var s1 = '00123'; var s2 = '000assa'; var s3 = 'assa34300'; var s4 = 'ssa'; var s5 = '121212000'; alert(s1 + '=' + trimNumber(s1)); alert(s2 + '=' + trimNumber(s2)); alert(s3 + '=' + trimNumber(s3)); alert(s4 + '=' + trimNumber(s4)); alert(s5 + '=' + trimNumber(s5)); // end hiding contents --> </script> 
+3
Nov 29 '11 at 4:18
source share
 parseInt(value) or parseFloat(value) 

This will work well.

+3
Oct 06 '15 at 13:09 on
source share

Try it,

  function ltrim(str, chars) { chars = chars || "\\s"; return str.replace(new RegExp("^[" + chars + "]+", "g"), ""); } var str =ltrim("01545878","0"); 

More here

+2
Nov 26 '11 at 5:19
source share

You should use the "radix" parameter of the parseInt function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FparIntInt

parseInt ('015', 10) => 15

if you are not using it, some javascript engine might use it as an octal parseInt ('015') => 0

+1
Oct 08 '13 at 12:26
source share

If the int number is used

 "" + parseInt(str) 

If the number uses float

 "" + parseFloat(str) 
+1
May 24 '16 at
source share

Just try multiplying by one as follows:

"00123" * 1; // Get as a number
"00123" * 1 + ""; // Get as a string

+1
Feb 10 '19 at 15:09
source share

In another way without regular expression:

 function trimLeadingZerosSubstr(str) { var xLastChr = str.length - 1, xChrIdx = 0; while (str[xChrIdx] === "0" && xChrIdx < xLastChr) { xChrIdx++; } return xChrIdx > 0 ? str.substr(xChrIdx) : str; } 

With a short line, it will be faster than a regular expression ( jsperf )

0
Apr 20 '17 at 11:31 on
source share
 const input = '0093'; const match = input.match(/^(0+)(\d+)$/); const result = match && match[2] || input; 
0
Feb 27 '19 at 17:41
source share



All Articles