Javascript: Decimals still being allowed in regex check for numbers [duplicate] - javascript

This question already has answers here:
How do I include negative decimal numbers in this regular expression?
(17 answers)
Closed 3 years ago.
I am using the following to test for only numbers but I am having an issue with decimals still passing the test /^-{0,1}\d*\.{0,1}\d+$/.test(v) . Am I using this incorrectly to test for that?

if you want only integers, use:
/^-?\d+$/.test(v)
--- EXPLANATION
^ - start of the string
-? - minus in the start, ? means can be but not necessary
\d - any number (including zero)
+ - the last expression (=any number) can repeat more then once
$ - end of the string

Related

Regular Expression in JavaScript having exactly 6 digits and at least two different digits [duplicate]

This question already has answers here:
Regular expression limit string size
(3 answers)
Closed 2 years ago.
I want to make sure that I have exactly 6 digits and not all of them are the same (222222 is not acceptable but 122222 or 211111 or 122223 are acceptable).
I am using this:
^(?=\d{6})(\d)\1*(?!\1)\d+$
But this only satisfies minimum 6 digits and not exactly 6 digits.
(source: Regex to match number with different digits and minimum length)
You may use this bit simplified and more efficient regex:
^(\d)(?!\1+$)\d{5}$
RegEx Demo
RegEx Details:
^(\d): Match and capture a digit at the start
(?!\1+$): Negative lookahead to assert that same digit is not repeated till end.
\d{5}$: Add 5 more digits before end

Is it possible to construct a regular expression that matches numbers with thousands and decimals delimiters exchanged such as 1,000.00 and 1,000.00? [duplicate]

This question already has answers here:
Regular expression to match numbers with or without commas and decimals in text
(11 answers)
Regex for optional comma separated number with decimal point
(1 answer)
Regex a decimal number with comma
(3 answers)
RegEx validation of decimal numbers with comma or dot
(6 answers)
Closed 3 years ago.
I am trying to build a regular expression in Javascript that matches numbers with , and . as delimiters of thousands and decimals and vice versa. It must match 1,000,000.00 and 1.000.000,00. So far I have this:
/^(\d{1,3}(?:([,\.])\d{3}){1}(?:\2\d{3})*|\d+)(?:(?!\2)[,\.](\d*))?$/
but it does not match when the number does not have thousands delimitir and have decimals delimitir such as 100.00 or 100,00 for example. I think it's because the capturing group of the thousands delimiter is null.
Is it possible to build such regular expression?

Why does javascript Number ending with a dot returns a number but when within a regex function it fails [duplicate]

This question already has answers here:
Javascript casts floating point numbers to integers without cause
(2 answers)
How to validate digits (including floats) in javascript
(10 answers)
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
Any whole number ending in a dot returns the number in javascript console.(except decimal numbers)
like > 1. returns 1. Adding >1+1. also works. I don't understand why
typeof(1) // 'number'
typeof(1.) //'number'
However, when I put the same number inside a function, regex test gives a wrong output.
i.e,
const regex = /^\d+$/ //checks if there is a number inside a string
regex.test('1') // true
regex.test(1) //true
regex.test('1.') // false
The workaround I have is simply regex.test(Number('1.'))
JavaScript has a single type for all numbers: it treats all of them as floating-point numbers. However, the dot is not displayed if there are no digits after the decimal point:
5.000 = 5
Also, \d matches a digit, not a number.

Regex for a decimal number [duplicate]

This question already has answers here:
Displaying a number in Indian format using Javascript
(15 answers)
Closed 9 years ago.
I used the following regex
var x=32423332.343;
var res= x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
which gives an output of 32,423,332.343
How do I modify this regex (shortest way) to get the following output
3,24,23,332.343
Well, if you want that, you can modify your regex a bit:
\B(?=(?:\d{2})*\d{3}(?!\d))
regex101 demo
(?:\d{2})* will match even number of digits before the final \d{3}.
For PCRE engine, one that can handle integers and floating, with g enabled.
\G\d{1,2}\K\B(?=(?:\d{2})*\d{3}(?!\d))

Regular expression for numbers with decimal [duplicate]

This question already has answers here:
Regular Expression to accept only positive numbers and decimals
(6 answers)
Closed 9 years ago.
In my phonegap app I need to validate the textbox with regular expression which should contain only numbers and two decimal places.
Here is sample I have tried:
rexp: /^-?\d+\.?\d*$/
This code allows only numbers but after the dot(.) its accepting many numbers. But I should allow only two. Please guide me to solve this issue.
You can make the number of matches explicit using the "{x}" modifier:
rexp: /^-?\d+\.?(\d{2})?$/
I suspect that you want to only have the two digits when there is a dot specified. Then you would modify you regexp as follows:
rexp: /^-?\d+(\.\d{2})?$/
I have created a JSFiddle to show the workings of the regexes and the difference between them: http://jsfiddle.net/q8NAz/

Categories

Resources