Javascript length limit of regex with given decimal conditions - javascript

I could make Javacsript regex with 2 decimal points with 3 integer but the thing is I have to make it 6 digits total including the decimal "dot".
So this result should be a minimum 0 to 999.99 with the condition it's a minimum 0 to 6 digit length.
Below is my solution So far:
^(\d{0}(?:\d{1,3})|\d{1}\.(?:\d{1,2})|\d{2}\.(?:\d{1,2})|\d{3}\.(?:\d{1,2}))$
Explanation:
If it's 0 digits, then min 1 to 3 digits so I can make it max 999.
If it's 1 digit w/ decimal then max 2 decimal points (ex) 2.22
If it's 3 digits w/ decimal then max 2 decimal points. (ex) 3.33
This is for regex in my JSP input (which is text type) and I'm literally suffering for this problem for days.
(this regex is for versions to be specific..)
Any help or better alternative way would help me A LOT.
google search, try by myself, online course, Youtube

If I get it well: you need to match a decimal with a length of 6 including the decimal point.
The following can do:
^(\d{1,3}(?:\.\d{1,2})?)$
It specified a integer part up to 3 digits, then an optional decimal part.
It has some edge cases of course such as matching: 000.00 which can be cleaned up further if needed depending on your expected input.
Sample: https://regex101.com/r/LrOHvt/1

Related

toPrecision is adding up trailing zeros after the decimal point to total numbers intended

After a long hour search, I'm asking this question.
I've a mission to restrict number to just 8 numbers in total. toPrecision is working as intended except this:
Some test cases:
4828.39019785 -> 4828.3902 (intended)
0.39019785 -> 0.39019785 (Although it returns total 9 digits, but it kinda work)
0.0039019785 -> 0.0039019785 (Not intended, returning 11 digits)
It seems that toPrecision isn't working what it intended i.e to return the exact number we wanted. It adds up the zeros before the decimal and trailing zeros after the decimal into the total number of digits.
My intention:
0.39019785 -> 0.3901978 (exactly 8 digits including 0)
0.0039019785 -> 0.0039019 (exactly 8 digits including 3 zeros)
var num1 = 0.39019785;
var num2 = 0.0039019785;
const exactPrecision = (number, precision) => number.toPrecision(precision).replace(new RegExp("((\\d\\.*){"+precision+"}).*"), '$1');
console.log(exactPrecision(num1, 8))
console.log(exactPrecision(num2, 8))
Hope that helps!
toPrecision doesn't do what you want when there're leading 0's so I created a function which gets rid of what's not needed afterwards.
It's quite a while since he OP asked this question, but i came along a similar issue, so this is my proposal for a clean solution without any regex-magic:
function fiveDigits(x) {
return x<1?x.toFixed(4):x.toPrecision(5);
}
In the case of leading zeroes, it will use toFixed() to limit the number of decimal places after the decimal separator. In case of bigger numbers without a 0 before the decimal separator, it rounds to the desired number of significant digits.
Corner Case: There is a caveat of course: In case you have large numbers that need more digits before the decimal separator, then the function yields a number string in exponential notation which of course eats up more characters.
function fiveDigits(x) {
return x<1?x.toFixed(4):x.toPrecision(5);
}
console.log(fiveDigits(123.456));
// expected output: "123.46"
console.log(fiveDigits(0.004));
// expected output: "0.0040"
console.log(fiveDigits(1.23e5));
// expected output: "1.2300e+5"
For me this solution works as intended and cleanly.
Cheers!
You can use toPrecision() function.
For example:
console.log(Number(1).toPrecision(8));
This will give you "1.0000000"

Binary computation using Regex in Javascript

I am having trouble figuring these questions out.
I want to use regex in JS to do this.
1.All binary strings of odd length containing alternating 0's and 1's.
2.All binary strings over 0 and 1 representing numbers greater than 5 when interpreted as binary numbers.
3.All binary strings over 0 and 1 representing numbers which are evenly divisible by 4 when interpreted as binary numbers.
4.All binary strings of length less than or equal to 5 containing only 0's and 1's where the number of 0's is equal to the number of 1's.
Any help is greatful.
I think that RegExps aren't the best solutions to your problems, but here they are:
\b(1(01)*|0(10)*)\b try in regexr
\b([01]*11[01]|0*1+[01]*[01]{3})\b try in regex
\b[01]*00\b try in regexr
\b((01|10){1,2}|0011|1100)\b try in regexr
I split this into 4 Regexes to easier understand:
1.
^(?:(?:10)+1|(?:01)+0)+$
This matches one or more of '01' or '10', followed by the opposite char, repeated one or more times.
2.
[01]{4,}$
This simply checks that the number ends with 4 binary digits.
3.
[01]*0{2,}$
This makes sure the number ends with 2 zeros.
4.
^(01|10|0011|0101|0110|1001|1010|1100)$
This simply OR's the different possibilities. Since there has to be equal zeros and '1' there can only be 2 or 4 digits in the number.
If you need it all in one regex you can propably OR them all together.

angularjs ng-pattern regex for 4 digits and 2 decimal

I am trying to create a Regex for a number with maximum 4 digits and if the input has decimal it has to have 2 digits - .20 and not .1.
tried:
ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/" //fail for 666666, .10, .1
Examples for valid inputs:
100.10
100
3000.10
Example for invalid:
10000 //has more then 4 digits before decimal
100.1 //has only 1 digit after decimal
.10 //has no digits before decimal
Thanks for any help.
Use {#,#} to limit the number of digits to 1 to 4
Try
^[0-9]{1,4}(\.[0-9][0-9])?$
Use ( )? to make an optional two-digit decimal part
The problem with using the {1,2} is that it allows one or two digits, when you really only want two. And I assume you want to enforce a rule that if they have a ".", they must have two digits?
For example
var patt = /^[0-9]{1,4}(\.[0-9][0-9])?$/i
"1011.11".match(patt)!==null
"1011.1".match(patt)!==null
Returns
true
false
With gratitude to Sebastian Proske and Wiktor Stribiżew
For pointing out the need to escape the .

Javascript regex positive less than 10000 than can be decimal

I need a regex for a number than can be decimal and less than 10000 with max 2 digits after decimal.
I tried
/^([0-9]{1,4})+(\.[0-9]{0,2})$/
but it returns true for 44555.54 for example.
In your regex you are using + which is using for one or more repetition and which leads to match any length of digit and make decimal part non-greedy(using?) to make it optional.
^[0-9]{1,4}(\.[0-9]{0,2})?$
or using \d for digit character class.
^\d{1,4}(\.\d{0,2})?$

Regular Expression For Decimal with precision 5 and scale 2

I am trying to validate a text box on key press and here is my criteria.
I need a regular expression which allows total of 5 digits and a decimal point.
If there's no decimal point then it should allow to enter 5 digits.
And if there's a decimal point then it should allow only 2 digits after the decimal point.
However the total number of digits should not exceed 5 digits excluding decimal point and it can be less than 5 digits.
And below is my regex
/^(?:\d{1,2}(?:\.\d{0,6})?)?$/
I hope I am clear and let me know if any clarifications needed
Please help Thanks in advance.
Here's a regex that just lists out alternatives separated by |:
/^\d{1,5}|\d{1,4}\.\d|\d{1,3}\.\d{2}$/
i.e.
1-5 digits with no decimal point OR 1-4 digits followed by a decimal point and one digit after the decimal OR 1-3 digits followed by a decimal point and two digits after the decimal.

Categories