Remove/ truncate leading zeros by javascript/jquery
You can use a regular expression that matches zeroes at the beginning of the string:
s = s.replace(/^0+/, '');
Remove leading zeros from a number in Javascript
We can use four methods for this conversion
- parseInt with radix
10
- Number Constructor
- Unary Plus Operator
- Using mathematical functions (subtraction)
const numString = "065";
//parseInt with radix=10let number = parseInt(numString, 10);console.log(number);
// Number constructornumber = Number(numString);console.log(number);
// unary plus operatornumber = +numString;console.log(number);
// conversion using mathematical function (subtraction)number = numString - 0;console.log(number);
javascript remove leading zeros from a number
Because your value starts with 0, and its type is number instead of string, it will be recognized as octal, and when you use it directly, javascript will convert it to decimal
The fastest way you can use Number.prototype.toString(radix)
let a = 043
console.log(a.toString(8))
How to remove leading zeros from a number within a function parameter
The reason this is happening is because leading a number with a zero makes javascript interpret the number in octal format. There is no way that you can change this interpretation, but if you're getting the value from somewhere as a string you could use parseInt(string, 10)
We can do this string conversion ourselves and then parse it in base 10 like so:
let number = 0011console.log(parseInt(number.toString(8),10))
JS remove leading zeros
you can do that by a simple regex replace.
var number = "0.00558";
number = number.replace(/^[0\.]+/, "");
console.log(number);//number is now "558"
Remove leading zeros from input type=number
just use a regular expression like this
textboxText= textboxText.replace(/^0+/, '')
Remove leading zeros of a string in Javascript
The simplest solution would probably be to use a word boundary (\b
) like this:
s.replace(/\b0+/g, '')
This will remove any zeros that are not preceded by Latin letters, decimal digits, underscores. The global (g
) flag is used to replace multiple matches (without that it would only replace the first match found).
$("button").click(function() { var s = $("input").val(); s = s.replace(/\b0+/g, ''); $("#out").text(s);});
body { font-family: monospace; }div { padding: .5em 0; }#out { font-weight: bold; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div><input value="02-03, 02&03, 02,03"><button>Go</button></div><div>Output: <span id="out"></span></div>
How to remove leading zeros from the Array of Numbers in Javascript
With leding zero, you use octal numbers. To get decimals, you need to convert back to octals and parse this values as decimal numbers.
This approach works only on an array of octal numbers. It is not possible, to check if a value is taken from an octal literal.
let array = [000232, 0043, 0000454523, 0234], values = array.map(n => parseInt(n.toString(8), 10));
console.log(values);
regex: remove leading zeros, but keep single zero
You ay use this regex replacement:
.replace(/^(?:0+(?=[1-9])|0+(?=0$))/mg, '')
RegEx Demo
RegEx Details:
^
: Start(?:
: Start capture group0+(?=[1-9])
: Match 1 or more zeroes that must be followed by1-9
|
: OR0+(?=0$)
: Match 1 or more zeroes that must be followed by one0
and end
)
: End capture group
Replacement is empty string which will leave a single 0
if there are only zeroes in string otherwise will remove leading zeroes.
Alternative solution using a capture group:
str = str.replace(/^0+(0$|[1-9])/mg, '$1');
Related Topics
Overriding a JavaScript Function While Referencing the Original
Difference Between Window.Location.Href=Window.Location.Href and Window.Location.Reload()
Array.Push() If Does Not Exist
How to Handle the 'Onkeypress' Event in Reactjs
Set Http Header for One Request
How to Simulate Key Presses or a Click with JavaScript
How to Pass the This Context to a Function
How to Solve 'Redirect Has Been Blocked by Cors Policy: No 'Access-Control-Allow-Origin' Header'
Uncaught Typeerror: Cannot Read Property 'Msie' of Undefined - Jquery Tools
Internet Explorer 11 Detection
Encrypt in PHP Openssl and Decrypt in JavaScript Cryptojs
How to Return a View from an Ajax Call in Laravel 5
How to Debug Jquery Ajax Calls
How to Append a Div Inside an Svg Element