Regular expression for thousand separator without decimal point - javascript

I have regular expression which is working fine for decimal .
In below expression I want to remove decimal match to validate Integer
^-?(\d*)(,\d{1,3})*(?:[,]|([.]\d{0,2}))?$
Valid Match : 1,000 & 111, & -1000,00
Invalid Match : -,1 & 1,,,,

Use
^-?(?:\d{1,3}(?:,\d{3})*|\d+)?,?$
|---1---|
|---2-----|
|-3-|
The ^ asserts the position at the start of the string and $ asserts the position at the string end. -? matches an optional -. Part 1 matches 1 to 3 digits, Part 2 matches 0+ sequences of , followed with 3 digits, part 3 is an alternative to Part 1 & 2 combined and matches just 1+ digits. The whole number pattern is optional since the (?:...|...) is quantified with ? - one or zero occurrences.
The ,? before $ allows an optional comma at the end, delete if not required (you mentioned you need to allow 100, in the comments).
See the regex demo

Related

Limit 10 characters is numbers and only 1 dot

I'm having a regex problem when input
That's the requirement: limit 10 characters (numbers) including dots, and only 1 dot is allowed
My current code is only 10 characters before and after the dot.
^[0-9]{1,10}\.?[0-9]{0,10}$
thank for support.
You could assert 10 chars in the string being either . or a digit.
Then you can match optional digits, and optionally match a dot and again optional digits:
^(?=[.\d]{10}$)\d*(?:\.\d*)?$
The pattern matches:
^ Start of string
(?=[.\d]{10}$) Positive lookahead, assert 10 chars . or digit till the end of string
\d* Match optional digits
(?:\.\d*)? Optionally match a `. and optional digits
$ End of string
See a regex demo.
If the pattern should not end on a dot:
^(?=[.\d]{10}$)\d*(?:\.\d+)?$
Regex demo
The decimal point throws a wrench into most single pattern approaches. I would probably use an alternation here:
^(?:\d{1,10}|(?=\d*\.)(?!\d*\.\d*\.)[0-9.]{2,11})$
This pattern says to match:
^ from the start of the number
(?:
\d{1,10} a pure 1 to 10 digit integer
| OR
(?=\d*\.) assert that one dot is present
(?!\d*\.\d*\.) assert that ONLY one dot is present
[0-9.]{2,11} match a 1 to 10 digit float
)
$ end of the number
You can use a lookahead to achieve your goals.
First, looking at your regex, you've used [0-9] to represent all digit characters. We can shorten this to \d, which means the same thing.
Then, we can focus on the requirement that there be only one dot. We can test for this with the following pattern:
^\d*\.?\d*$
\d* means any number of digit characters
\.? matches one literal dot, optionally
\d* matches any number of digit characters after the dot
$ anchors this to the end of the string, so the match can't just end before the second dot, it actually has to fail if there's a second dot
Now, we don't actually want to consume all the characters involved in this match, because then we wouldn't be able to ensure that there are <=10 characters. Here's where the lookahead comes in: We can use the lookahead to ensure that our pattern above matches, but not actually perform the match. This way we verify that there is only one dot, but we haven't actually consumed any of the input characters yet. A lookahead would look like this:
^(?=\d*\.?\d*$)
Next, we can ensure that there are aren't more than 10 characters total. Since we already made sure there are only dots and digits with the above pattern, we can just match up to 10 of any characters for simplicity, like so:
^.{1,10}$
Putting these two patterns together, we get this:
^(?=\d*\.?\d*$).{1,10}$
This will only match number inputs which have 10 or fewer characters and have no more than one dot.
If you would like to ensure that, when there is a dot, there is also a digit accompanying it, we can achieve this by adding another lookahead. The only case that meets this condition is when the input string is just a dot (.), so we can just explicitly rule this case out with a negative lookahead like so:
(?!\.$)
Adding this back in to our main expression, we get:
^(?=\d*\.?\d*$)(?!\.$).{1,10}$

Javascript Regular Expresion [duplicate]

I'm trying to write a RegExp to match only 8 digits, with one optional comma maybe hidden in-between the digits.
All of these should match:
12345678
12,45678
123456,8
Right now I have:
^[0-9,]{8}
but of course that erroneously matches 012,,,67
Example:
https://regex101.com/r/dX9aS9/1
I know optionals exist but don't understand how to keep the 8 digit length applying to the comma while also keeping the comma limited to 1.
Any tips would be appreciated, thanks!
To match 8 char string that can only contain digits and an optional comma in-between, you may use
^(?=.{8}$)\d+,?\d+$
See the regex demo
The lookahead will require the string to contain 8 chars. ,? will make matching a comma optional, and the + after \d will require at least 1 digit before and after an optional comma.
If you need to match a string that has 8 digits and an optional comma, you can use
^(?:(?=.{9}$)\d+,\d+|\d{8})$
See the regex demo
Actually, the string will have 9 characters in the string (if it has a comma), or just 8 - if there are only digits.
Explanation:
^ - start of string
(?:(?=.{9}$)\d+,\d+|\d{8}) - 2 alternatives:
(?=.{9}$)\d+,\d+ - 1+ digits followed with 1 comma followed with 1+ digits, and the whole string matched should be 9 char long (8 digits and 1 comma)
| - or
\d{8} - 8 digits
$ - end of string
See the Java code demo (note that with String#matches(), the ^ and $ anchors at the start and end of the pattern are redundant and can be omitted since the pattern is anchored by default when used with this method):
List<String> strs = Arrays.asList("0123,,678", "0123456", // bad
"01234,567", "01234567" // good
);
for (String str : strs)
System.out.println(str.matches("(?:(?=.{9}$)\\d+,\\d+|\\d{8})"));
NOTE FOR LEADING/TRAILING COMMAS:
You just need to replace + (match 1 or more occurrences) quantifiers to * (match 0 or more occurrences) in the first alternative branch to allow leading/trailing commas:
^(?:(?=.{9}$)\d*,\d*|\d{8})$
See this regex demo
You can use following regex if you want to let trailing comma:
^((\d,?){8})$
Demo
Otherwise use following one:
^((\d,?){8})(?<!,)$
Demo
(?<!,) is a negative-lookbehind.
/^(?!\d{0,6},\d{0,6},\d{0,6})(?=\d[\d,]{6}\d).{8}$/
I guess this cooperation of positive and negative look-ahead does just what's asked. If you remove the start and end delimiters and set the g flag then it will try to match the pattern along decimal strings longer than 8 characters as well.
Please try http://regexr.com/3d63m
Explanation: The negative look ahead (?!\d{0,6},\d{0,6},\d{0,6}) tries not to find any commas side by side if they have 6 or less decimal characters in between while the positive look ahead (?=\d[\d,]{6}\d) tries to find 6 decimal or comma characters in between two decimal characters. And the last .{8} selects 8 characters.

“combine” 2 regex with a logic or?

I have two patterns for javascript:
/^[A-z0-9]{10}$/ - string of exactly length of 10 of alphanumeric symbols.
and
/^\d+$/ - any number of at least length of one.
How to make the expression of OR string of 10 or any number?
var pattern = /^([A-z0-9]{10})|(\d+)$/;
doesn't work by some reason. It passes at lest
pattern.test("123kjhkjhkj33f"); // true
which is not number and not of length of 10 for A-z0-9 string.
Note that your ^([A-z0-9]{10})|(\d+)$ pattern matches 10 chars from the A-z0-9 ranges at the start of the string (the ^ only modifies the ([A-z0-9]{10}) part (the first alternative branch), or (|) 1 or more digits at the end of the stirng with (\d+)$ (the $ only modifies the (\d+) branch pattern.
Also note that the A-z is a typo, [A-z] does not only match ASCII letters.
You need to fix it as follows:
var pattern = /^(?:[A-Za-z0-9]{10}|\d+)$/;
or with the i modifier:
var pattern = /^(?:[a-z0-9]{10}|\d+)$/i;
See the regex demo.
Note that grouping is important here: the (?:...|...) makes the anchors apply to each of them appropriately.
Details
^ - start of string
(?: - a non-capturing alternation group:
[A-Za-z0-9]{10} - 10 alphanumeric chars
| - or
\d+ - 1 or more digits
) - end of the grouping construct
$ - end of string

Generic JavaScript regex validating a positive number with or without commas as thousand separators and an optional fractional part

I have following regex to validate numbers in input
var reg = /^\d+$/;
Now i want to allow ,(commas) and .(period) in number field as following will some one help me writing regex to allow following number format ?
10000000
10,000,000
10000000.00
You may use
/^(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?$/
See the regex demo
If you only need to allow 2 digits after the decimal separator, replace (?:\.\d+)? with (?:\.\d{1,2})?.
Details:
^ - start of string
(?:\d{1,3}(?:,\d{3})*|\d+) - 2 alternatives:
\d{1,3}(?:,\d{3})+ - 1 to 3 digits and one or more sequences of a comma and 3 digits
\d+ - 1 or more digits
(?:\.\d+)? - an optional sequence of:
\. - a dot
\d+ - 1 or more digits
$
You could use
^((\d{1,2}(,\d{3})+)|(\d+)(\.\d{2})?)$
see Regex101
or
^((\d{1,2}(,\d{3})+)|(\d+))(\.\d{2})?$
if you want 10,000,000.00 to get matched to.

Regex for phone numbers that check if numbers are not the same and last 7 digits are not same

I am trying to create a regex which matches 7-15 digit number, the number cannot contain all the same digits and Last 7 digits cannot be the same. I have made two regex expressions for number that all numbers cannot be same. The regex which I have made is:
/^(?!(.)\1+$)^(|[0-9]{7,15})$/.
And for Last seven digits cannot be same,the regex which i have made is:
/^(?!.*(\d)\1{6}\b)^[0-9]{0,15}$/.
But the problem is I am not able to make the regex which full fills both the conditions i.e. all the numbers cannot contain all the same digits and Last 7 digits cannot be the same.
Please suggest how this can be done.
It seems you can use alternation operator inside the negative lookahead to check for 2 conditions:
^(?!(\d)\1+$|\d*(\d)\2{6}$)(?:\d{7,15})?$
See the regex demo.
Details:
^ - start of string
(?!(\d)\1+$|\d*(\d)\2{6}$) - the negative lookahead failing the match if all digits are the same from start to end ((\d)\1+$ where (\d) captures a digit into Group 1 and then \1+ matches one or more values captured in Group 1 followed with end of string check with $), or if only the last 7 are the same (see \d*(\d)\2{6}$ where \d* matches 0+ digits, (\d) captures a digit into Group 2 and then \2{6} matches 6 values captured in Group 2 followed with end of string check with $)
(?:\d{7,15})? - an optional group matching 7 to 15 digits (or an empty string, as it's optional)
$ - end of string.

Categories

Resources