javascript search and replace prefix zero - javascript

i have few numbers like
0011,0101,0123,1234,5245,0052,3265,0047,0124
How replace prefix zero only,
like no number should start with zero ,
exactly like
0011 should be 11 ,
0101 should be 101 ,
0123 should be 123
How to do this ?
Is ther any javascript function there ,
Thanks

parseInt("0011", 10);
will return a Number with the value of 11. Note that the second argument is important, otherwise your zero-prefixed numbers would be interpreted as octal.

Related

Match exacltly n numeric digits Regex

I would like to use regex to match a string of exactly 2 characters, and both of those characters have to be between 0 and 9.
Example: if I provide 12 - True and if I provide 123- it should give false.
I have tried below examples.
\d{6}$
^[0-9]{2}$
However, Even I enter 123, it is giving as true but I need it as false as I need exactly two.
try this regex
^\d\d$
test here https://www.regextester.com/1966
Using ^[0-9]{2}$ you get a number between 0 and 9 and then two digits.
If you only want two digits can use \d{2}
I think the best way is to use wc -l to count the amount of charecters.
i.e:
NUM=123
if [[ `echo ${NUM} | wc -l` -gt 2 ]];then
echo "You provided a number grater than 2 digits"
exit 1
fi

Javascript evaluate number starts with zero as a decimal

I want to evaluate number starts with zero as a decimal number.
For example, let's define convertToDec
convertToDec(010) => 10
convertToDec(0010) => 10
convertToDec(0123) => 123
etc..
Because all js numbers starts with 0 are evaluated in base 8, I tried to do it like this:
function convertToDec(num){
return parseInt(num.toString(), 10);
}
But the toString function parses the number in base 8.
Any suggestions?
Thanks!
If you literally write 0010 in JavaScript, then it will be treated as an octal number. That's just how the parser works.
From MDN's docs:
Decimal integer literal consists of a sequence of digits without a leading 0 (zero).
Leading 0 (zero) on an integer literal indicates it is in octal. Octal integers can include only the digits 0-7.
Leading 0x (or 0X) indicates hexadecimal. Hexadecimal integers can include digits (0-9) and the letters a-f and A-F.
Leading 0b (or 0B) indicates binary. Binary integers can include digits only 0 and 1.
So, when you write convertToDec(0010), your browser interprets this as convertToDec(8). It's already been "converted" to an 8 since you used an "octal literal".
If you want the literal value "0010", then you'll need to use a string.
parseInt("0010", 10); // 10
You need to call convertToDec with string arguments, not numbers.
function convertToDec(num){
return parseInt(num, 10);
}
alert(convertToDec("010"));
If you give it a number as the argument, the number has already been parsed by the Javascript interpreter, the function can't get back what you originally typed. And the JS interpreter parses numbers beginning with 0 as octal.

Why 0010 in javascript is equal to 8?

i wrote from 001 to 0010 and much more digit like this that started with "00" in chrome console and Fire Fox even in IE and get this result.
why 0010 is not equal to 10 ?
or why 0020 is not equal to 20 ? and it is "16".
A leading zero indicates that a number should be interpreted as octal.
Thus 10 interpreted as octal is equal to 8 in decimal.
For more information refer to MDN on number literals.
"Numeric constants are considered octal if they are preceded by a zero, and are considered hexadecimal if they are preceded by a zero and and x (0x)." (as explained here)
008 is not considered octal because it contains "8" which is not an octal number. 0010 is in fact an octal number and equals 8.
Number literals in Javascript can be entered in different bases -
a leading zero means the number is the number is in octal base (only digits 0-7) so 010 is the same as: one times 8 + zero
the literal 0x10 is in hexadecimal (base 16) so equals to: one times 16 + zero) = 16
see here https://developer.mozilla.org/en/docs/JavaScript/Guide/Values,_variables,_and_literals
Because the leading 0 represents an Octal number system. Likewise, if you had typed 0x010 it would equal to 16, since 0x is prefix for Hexadecimal number system.

Age-verifying regex that should reject 3-digit numbers that begin with zero returning something strange

I'm attempting to write a piece of regex code that verifies an age that has 1,2, or 3 digits for a simple form. If it has 3 digits, the leading digit must be a 1, and the leading digit is never 0.
What I have so far:
/^1[0-9][0-9]$|^[0-9][0-9]$|^[0-9]$/
An example of this odd behavior:
> myRe = /^1[0-9][0-9]$|^[0-9][0-9]$/;
> myRe.exec(023);
["19"]
> myRe.exec(052);
["42"]
023 is a number, and regular expressions don't work on numbers. So JavaScript converts the number into a string first. And because of the zero, it is an octal number; so 023 is actually 2 * 8 + 3, and not 2 * 10 + 3.
You want to do myRe.exec("023") instead. Also, you might want to modify your regex so that the first digit in the double-digit branch and the single digit in the last branch read [1-9], and not [0-9].
If you start a number literal with 0 it's interpreted as octal (base 8). So 052 (base 8) is actually 42 (base 10). Which is then converted to string ("42") and regexed.
As far as your regex goes: I'd improve it a bit like so:
/^1[0-9][0-9]$|^[1-9][0-9]$|^[0-9]$/
In the expression
myRe.exec(023);
023 is an octal number. What you want to check is a string. So enclose it in quotes
/^1?\d{1,2}$/ - and here is a shorter and more readable version of the same regex.
Or /^1?\d?\d$/ whichever you like more.
You're appplying your RegExp against a number. RegExps expect a string argument.
Therefore:
> myRe = /^1[0-9][0-9]$|^[0-9][0-9]$/;
> myRe.exec('023');
null
> myRe.exec('23');
["23"]
> myRe.exec('052');
null
> myRe.exec('52');
["52"]
As expected.

regex to detect leading zeros and check the lenght from 0 to 12 digits

Hi I have a javascript function that checks for signed integer with 0 to 12 length, i also want to see if there are any leading 0's like 0012 should return false.
function sInteger0to12(str) {
str = str.replace(/^\s+|\s+$/g, '');
return /^[-+]?\d{0,12}$/.test(str);
}
any help will be appreciated.
I'm assuming that the following should match:
1
+1
-1
0
-123456789012
<empty>
And these should fail:
-
+
01
-01
1234567890123
00
+0
-0
If you disagree with my decisions above, please let me know and I will try to fix the regex.
Here's a regex you can use:
/^([-+]?[1-9]\d{,11}|0)?$/
Like this:
/^[-+]?[1-9]\d{0,11}$/
You'll need to check for '0' separately.
You need to cover three cases
an empty string
a single 0
a no zero padded 1 to 12 digit number
these cases equate to
^$
^0$
^[+-]?[1-9]\d{0,11}$
which adds up to
^()|(0)|([+-]?[1-9]\d{0,11})$
This should help:
/(^0$)|(^[+-]?[1-9]\d{0,11}$)/

Categories