I have one regexp which is finding only 2 digit numbers. I'm trying with \#break:[0-9][0-9]\s\minutes this regexp. It has only 2 digits. How I can rewrite this to detect any number even it 5 or 6 digits.
/\d+/ should do the trick.
"d" is the symbol for digits and "+" tells it to accept one or more.
Try it with d+ as Kevin said: \#break:\d+\s\minute
Or if you exactly know how many digits should be found, just use \#break:\d{1,5}\s\minute, which will catch digits from 1 to 5.
Related
$("#xyz").inputmask('Regex', {regex: "^[0-9]{1,8}(\\.\\d{1,2})?$"});
Above expression allows me to enter 8 digits and 2 decimals but it doesn't automatically handle commas. Can any one help me with commas.
Your regex is a bit wrong, this should work instead:
^[0-9]{1,8}([,.][0-9]{1,2})$
It will accept up to 8 digits and then a comma or dot, followed by up to another 2 digits.
Edit: This regex requires commas, decimals are optional. Maximum is 999,999,999, minimum is 0.00
^([0-9]{0,3}|0)(,[0-9]{3})?(,[0-9]{3})?(\.[0-9]{1,2})?$
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})?$
so I'm making this regular expression to verify some text boxes on a website that I'm designing for an internship.
The problem is that I'm not so keen on regular expressions, and I'm close to having a working one that matches a number between 0-24 and no more than two decimal places.
This is what I have so far. The pattern is also matching any string; such as, "a" or "az".
var pattern = "^([0-9]{0,2}?.?[0-9]{0,2}|1[0-9].?[0-9]{0,2}|2[0-4].?[0-9]{0,2})$";
To get a number between 0 and 24 (24 excluded) with optional up to two decimal places:
^(\d|1\d|2[0-3])(\.\d{1,2})?$
The decimal part:
\. - match the decimal dot
\d{1,2} - one or two digits
()? - makes it optional
The whole part:
\d - numbers 0-9
1\d - numbers 10-19
2[0-3] - numbers 20-23
(x|y|z) - one of x, y or z
As for the "why is my version matching things like "a" and "az" part" - it's a little complex, but it basically boils down to you using dots (like .?). In regex, a dot means "any one character". To make it match a literal dot, you need to escape it with a slash just like I did.
Minor remark: If you want optional leading zero for single digit numbers, replace 1\d with [01]\d. If you want mandatory leading zero for single digit numbers, replace \d|1\d with [01]\d. If you don't want leading zeroes, leave it as it is.
Assuming you do not want 05 or 5.50
^((?:[0-9]|1[0-9]|2[0-3])(?:\.(?:[1-9]|[0-9][1-9]))?)$
You can try it here
The following is a quick attempt to match a floating point number from 0 to 24.99 with up to two non-zero digits
^(([0-9])|([01][0-9])|(2[0-4]))(\.[0-9]{1,2})?$
I think it might be easier to use math to do this though...
You can see the explanation of the entire regex as well as test it out here. I have also added a few test cases.
^(\d|[01]\d|2[0-3])(\.\d{1,2})?$
Test cases:
Valid:
22
1.29
2.99
9.99
13.24
17.38
20.01
02.15
15.35
23.56
1.1
Invalid:
24.29
235.215
21.256
To get a integer number between 1 and 23: ^([1-9]|1[0-9]|2[0-3])$
I have a list of phone numbers which are formatted in multiple ways such as: (212)-555-1234 or 212-555-1234 or 2125551234.
Using JavaScript, what would be the best way to extract only the area code out of these strings?
First, remove everything that is not a digit to get the plain number. Then, get the first three digits via slicing:
return myString.replace(/\D/g,'').substr(0, 3);
Get the first 3 consecutive digits...
/[0-9]{3}/.exec("(212)-555-1234")[0]
Sample (fiddle):
console.log(/[0-9]{3}/.exec("(212)-555-1234")[0]); // 212
console.log(/[0-9]{3}/.exec("212-555-1234")[0]); // 212
console.log(/[0-9]{3}/.exec("2125551234")[0]); // 212
Take the first 3 digits of a 10 digit number, or the first 3 digits after the 1 of an 11 digit number starting with 1. This assumes your domain is U.S. phone numbers.
You can also use my library.
https://github.com/Gilshallem/phoneparser
Example
parsePhone("12025550104");
result: { countryCode:1, areaCode:202, number:5550104, countryISOCode:"US" }
Regex as '^\(*(\d{3})' should do it. Get the first group from the match.
Here ^ will start the match from beginning, \d{3} will match 3 digits. \(* will match the optional starting parenthesis. You don't need to care about next digit or symbols after the area code.
I use this to test for digits
/^\d+$/
But I need to make sure that it's greater than zero, while still allowing 0000123123123 for instance.
You can write:
/^\d*[1-9]\d*$/
(zero or more digits, followed by a nonzero digit, followed by zero or more digits).
It is correct regex for positive digits.
/^[1-9]\d*$/g
The previous answer not correct for 0123.