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/
Related
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
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
This question already has answers here:
Removing everything except numbers in a string
(7 answers)
Closed 3 years ago.
I want to replace the dot in a number in Javascript with regular expression; if country_temp is, for example, 1.234, how can I put the value 1234 in country_temp2?
I have tried the following:
const country_temp2 = country_temp.replace(/[a-zA-Z]*\**\s*\./g, "")
but it's not working.
Edit: the original string is composed by characters, asterisk, a number and a dot
Sorry, but I have written in a very fast way.
Try this:
country_temp.replace(/\./g,'')
This question already has answers here:
Comma Separated Numbers Regex
(6 answers)
Closed 6 years ago.
I am trying to generate a regex which would match the following sequence-
+911111111111,+912222222222,+913333333333,+914444444444
It should not allow any other character other than + and numbers
I have tried this->
/^(\+91)\d{10}$/
But it works only for one phone number not for multiple phone numbers
If
^\+\d{11}$
(a + followed by 11 digits) is not sufficient you'll need to be more specific about what you want to allow and not-allow.
Update following comment: the first two digits are "91" so those can be specified, and then ten further digits:
^\+91\d{10}$
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))