I am looking for regex expression which takes -10 to 10 with 2 decimal numbers upto 12. means cases passed are -10.01 to -10.11 for 10 years and 11 months.
I was able to make a regex for -10 to 10 but adding decimal till .11 was not able to figure out If somebody can help that would be great.
cases passed -
0
1
10.01
10.00
10.11
-10.00
-10.01
10.12
-10.00
10
cases should fail -
10.12
10.13
-10.13
11
-11
-10.13
I presume that values such as 9.08 and -5.04 are also valid? In that case this regex: ^-?(10|[0-9])(\.(0[0-9]|1[01]))?$ will match the values you want. The first part looks for a number from -10 to 10; the second part looks for an optional decimal part which can be something from 00-09 or 10 or 11.
Regex101 demo
Related
This question already has an answer here:
Regex for values between -10 and 10 up to 1 decimal place
(1 answer)
Closed 3 years ago.
Want to generate a regex for any integer between -20 and 150 including upto 2 decimals
E.g 36.50
Max value 150.00
and min value -20.00
Have tried this so far but its including -20.78 also and 150.04 also.
I want to limit it to 150.00 and -20.00
^((\-([1-9]|1[0-9]|20)(\.\d{2})?)|([0-9]|[1-8][0-9]|9[0-9]|100)(\.\d{2})?)$
Can you please help me out ?
I vote for just using an inequality, along with a simple regex check to assert that there is no decimal component more precise than hundreths:
var input = "10.35";
if (input >= -20 && input <= 150 &&
/^-?\d+(?:\.\d{1,2})?$/.test(input)) {
console.log("MATCH");
}
try this,
instead of taking 20 with all other make it separate
^((\-([1-9]|1[0-9])(\.\d{2})?)|((\-(20)(\.(00))|([0-9]|[1-8][0-9]|9[0-9]|100)(\.\d{2})?)$
I am trying to use RegExp() to accept the following inputs:
1234567890
12 34 56 78 90
12-34-56-78-90
12.34.56.78.90
12 34.56-7890
What I have been trying to use is:
([0-9]{2,2}[ |-|.]?){4,4}[0-9]{2,2}
Which I understand as: (digit 2 times, followed by space or - or . or nothing) 4 times, then digit 2 times.
I have been testing [0-9]{2,2} which doesn't even behave as I expected since it accepts at least 2 digits, and not exactly 2 digits.
This one should suit your needs (last case not matched as expected):
^\d{2}([ .-]?)\d{2}(?:\1\d{2}){3}$
Demo
var mob=/^([0-9]{2}(\s|\.|\-)){4}[0-9]{2}$/;
console.log(mob.test("12.34.56.78.90"));
console.log(mob.test("12-34-56-78-90"));
console.log(mob.test("12 34 56 78 90"));
var mob=/^([0-9]{2}(\s|\.|\-)?){4}[0-9]{2}$/;
console.log(mob.test("1234567890"));
console.log(mob.test("12 34 56 78 90"));
This question already has answers here:
Javascript - Leading zero to a number converting the number to some different number. not getting why this happening?
(2 answers)
Closed 6 years ago.
Why 2 + 10 = 12 and 2 + 010 = 10? I tried this on console of Google Chrome v 51.0 and IE 8 only to get the same results. Maybe this is feature of Javascript.
Somebody please help me understand the logic behind it and what possible good it can serve?
Sometime JavaScript treats numbers exactly as you specify them, other times it tries to 'interpret' the number.
In this case, JavaScript is interpreting the number.
The leading zero means that the number is octal -- base 8 -- where the digits go 0, 1, 2, 3, 4, 5, 6, 7.
So.. 010 (octal) == 8 (decimal)
2 + 010
2 + 8
10
If you are dealing with numbers from an outside source, you can use parseInt
2 + parseInt("010", 10);
Note: The ,10 is also important to force that number to be translated as base 10. In ES3 (and I believe 5) you may get different results without it, depending upon the browser.
Its not binary, the 0 at the start makes it octal, and octal 010 == 8
https://en.wikipedia.org/wiki/Octal
0 before a number tells javascript to interpret the number as octal. So 10 in octal means 8 in decimal and 8 + 2 is 10.
Numbers with leading 0 are considered as octal numbers. That is why 010 is interpreted as 8 hence you get 10 as sum of 2 + 010. This happens with python as well.
10 is equal to 8 in octal and the first zero tells javascript to interpret that as an octal number. So when 2 + 010 is executed, at first 010 is translated into 8 and then it is added to the 2 to give 10.
I wrote this regular expression for the Lebanese phone number basically it should start with
00961 or +961 which is the international code then the area code which
could be either any digit from 0 to 9 or cellular code "70" or "76" or
"79" then a 6 digit number exactly
I have coded the following reg ex without the 6 digit part :
^(([0][0]|[+])([9][6][1])([0-9]{1}|[7][0]|[7][1]|[7][6]|[7][8]))$
when i want to add code to ensure only 6 digits more are allowed to the expression:
^(([0][0]|[+])([9][6][1])([0-9]{1}|[7][0]|[7][1]|[7][6]|[7][8])([0-9]{6}))$
It Seems to accept 5 or 6 digits not 6 digits exactly
i am having difficulty finding whats wrong
use this regex ((00)|(\+))961((\d)|(7[0168]))\d{6}
Ths is what I would use.
/^(00|\+)961(\d|7[069])\d{6}$/
00 or +
961
a 1-digit number or 70 or 76 or 79
a 6-digit number
The [0-9]{1} will match also the cellular codes 7x since 7 is between 0 and 9. This means that a "5 digit cellular number" will match on a 7 and six more digits.
Try
/^(00961|\+961)([0-9]|70|76|79)\d{6}$/.test( phonenumber );
//^ start of string
// ^^^^^^^^^^^^^ 00961 or +0961
// ^^^^^^^^^^^^^^^^ a digit 0 to 9 or 70 or 76 or 79
// ^^^^^ 6 digits
// ^ end of string
The cellar code is forming a trap, as #ellak points out:
/^((00)|(\+))961((\d)|(7[0168]))\d{6}$/.test("009617612345"); // true
Here the code should breaks like this: 00 961 76 12345,
but the RegEx practically breaks it like this: 00 961 7 612345, because 7 is matched in \d, and the rest is combined, exactly in 6 digits, and matched.
I'm not sure if this is actually valid, but I guess this is not what you want, otherwise the RegEx in your question should work.
Here's a kinda long RegEx that avoids the trap:
/^(00|\+)961([0-68-9]\d{6}|7[234579]\d{5}|7[0168]\d{6})$/
A few test result:
/(00|\+)961([0-68-9]\d{6}|7[234579]\d{5}|7[0168]\d{6})/.test("009617012345")
false
/(00|\+)961([0-68-9]\d{6}|7[234579]\d{5}|7[0168]\d{6})/.test("009618012345")
true
/(00|\+)961([0-68-9]\d{6}|7[234579]\d{5}|7[0168]\d{6})/.test("009617612345")
false
/(00|\+)961([0-68-9]\d{6}|7[234579]\d{5}|7[0168]\d{6})/.test("0096176123456")
true
Just recently, the Lebanese Ministry of Telecommunication has changed area codes on the IMS. So the current Regex matcher becomes:
^(00|\+)961[ -]?(2[1245789]|7[0168]|8[16]|\d)[ -]?\d{6}$
Prefix: 00 OR +
Country code: 961
Area code: 1-digit or 2-digits; including 2*, 7*, 8*..., OR a single digit for Ogero numbers on the old IMS network starting with 0*, and finally older mobile lines starting with 03.
The 6-digit number
News on the961.com
I need a regular expression in javascript that will accept only positive numbers and decimals. This is what I have but something is wrong -- it doesn't seem to take single positive digits.
/^[-]?[0-9]+[\.]?[0-9]+$/;
For example, 9 will not work. How can I restructure this so if there is at least one positive digit, it will work?
/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/
matches
0
+0
1.
1.5
.5
but not
.
1..5
1.2.3
-1
EDIT:
To handle scientific notation (1e6), you might want to do
/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?$/
If you want strictly positive numbers, no zero, you can do
/^[+]?([1-9][0-9]*(?:[\.][0-9]*)?|0*\.0*[1-9][0-9]*)(?:[eE][+-][0-9]+)?$/
There are few different ways to do this depending on your need:
/^[0-9.]+$/ matches 1 and 1.1 but not -1
/^[0-9]+\.[0-9]+$/ matches 1.1 but not 1 or -1
Generally, I recommend using a simple regExp reference guide like http://www.regular-expressions.info/ for building expressions, and then test them using javascript right your browser console:
"123.45".match(/^[0-9.]+$/)
You can try this -
^\d{0,10}(\.\d{0,2})?$
Also one cool site to test as well as to get description of your own regular expressions
https://regex101.com/
How about like:
^[.]?[0-9]+[.]?[0-9]*$
I have found something interesting which work for me!!!
hope so for you also...!!!
[0-9].[0-9]
Regex for accepting only a positive number with a decimal point
Matches:
1, 2564, 2.5545, 254.555
Not Matches:
-333, 332.332.332, 66-665.455, 55554-4552
I have done one in case people are interested:
^([0-9]*)(.[[0-9]+]?)?$
but it doesn't have '+' sign
while also it doesn't accept '-' sign
It accepts:
0
0.5
1.0
1.5
2.0
2.5
3.0
1
2
3
4
0.0
0.1
0.9
0.4
1.3
1.11
1.51
2.01
01.5
0.51
1.1
.1
Reject:
-1.0
-0.5
-0.0
-0.1
-0.7
11123.
1.001.
.1.1