regex matching decimals in an amount [duplicate] - javascript

This question already has answers here:
Regex that matches numeric with up to 2 decimal places
(8 answers)
Closed 9 years ago.
I am new to regex and need to write a regex to match both "100" and "100.00" in an amount value. The last two digits are matched only if these are following a "."
I have tried : ^\d+?(?=([.]{1})\d{2})$ - without any luck
Help is much appreciated

You don't need a lookahead here. You can use an optional non-capture group
^\d+(?:\.\d{2})?$

Use this regex: /^\d+(?:\.\d{2})?$/

Variation on previous answers
^\d{1,8}([\.,]\d{1,2})?$
I've found that users will still enter money values as 1.5 when they mean 1.50, so we use a regex to allow for that as well.
The number of decimal places can be adjusted, as can the number of digits before the decimal point

Related

Regex length depending on value and decimal [duplicate]

This question already has answers here:
jQuery current regular expression
(2 answers)
Closed 1 year ago.
I'm trying to only allow the following numbers in my input:
if the input starts with 0-9 and a decimal follows then max length would be 4
ex. 0.99 or 1.23
if the input starts with 10-99 and a decimal follows then the max length would be 5
ex. 10.12 or 99.99
so far I have this and it works but not as strictly as I'd like
/^[0-9]+(\.[0-9]*)?$/
I believe this is what you are looking for. The {1,2} thing says it can have one or two of the instance before it. The \d specifies a digit.
/^\d{1,2}\.\d{1,2}$/
If you require two decimal places always:
/^\d{1,2}\.\d{2}$/
Try this:
console.log(/^[0-9][0-9]?(\.[0-9]{0,2})$/.test("0.98"));
console.log(/^[0-9][0-9]?(\.[0-9]{0,2})$/.test("0.987"));
console.log(/^[0-9][0-9]?(\.[0-9]{0,2})$/.test("00.98"));
console.log(/^[0-9][0-9]?(\.[0-9]{0,2})$/.test("00.987"));

Can regex find patterns like this? [duplicate]

This question already has answers here:
Regex to detect one of several strings
(7 answers)
Closed 2 years ago.
I am learning more and more about regex and while reading about it I was wondering if regex can do something like this. Looking in a string for, for example numbers that are 3 digits long, where the second digit is 2 higher than the first digit, and the third digit is 4 higher than the second digit.
For example:
Matches:
137,
248,
359
Would regex be capable of such thing? From my limited knowdlege it does not seem to be possible.
Regex is only capable of matching string, not performing mathematcial logic on numbers, so it is not able to determine that two digits differ by certain quantity.
So answer is no.
BUT, that rule limits 3-digit numbers to a few, so you can write regex alternation to do that:
(?:137|248|359)
Explanation:
(?:...) - non-cpaturing group
| - alternation

I need help in creating regex for JavaScript where minimum 10 digit number is allowed and maximum 15. It may optionally have "+" at the start only [duplicate]

This question already has answers here:
Phone number validation with plus sign optional
(2 answers)
Using explicitly numbered repetition instead of question mark, star and plus
(4 answers)
Closed 2 years ago.
I want to create regex for use in JavaScript where minimum 10 digit number is only allowed and maximum 15 digit. It may optionally have "+" at the start only and not at any other places.
You can use the following expression
^\+?\d{10,15}$
See a demo on regex101.com.
I believe this can do the work
var patt = new RegExp("^([+]([0-9]){10,15}$)|^(([0-9]){10,15})$");

How to write the regexp to accept infinite numbers?

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.

What is the most compact, efficient and reliable way of extracting numbers from dollar amounts in JavaScript? [duplicate]

This question already has answers here:
How to convert a currency string to a double with Javascript?
(23 answers)
Closed 7 years ago.
What I'm talking about is reading a string into a Number, e.g.
"$107,140,946" ---> 107140946
"$9.99" ---> 9.99
Is there a better way than
dolstr.replace('$','');
dolstr.replace(',','');
var num = parseInt(dolstr,10);
???
Using regex is much simpler to read and maintain
parseFloat(dolstr.replace(/\$|,/g, ""));
You can just put all of this in oneliner:
parseFloat(dolstr.replace('$','').split(",").join(""))
Notice that I do not replace the second one, because this will remove just the first ','.
Using a simple regex and the string's replace function
parseFloat(dolstr.replace(/[^\d\.]/g, ''))
Breakdown
It replaces every instance of a character that is not a digit (0 - 9) and not a period. Note that the period must be escaped with a backwards slash.
You then need to wrap the function in parseFloat to convert from a string to a float.
Assuming input is always correct, just keep only digits (\d) and the dot (\.) and get rid of other characters. Then run parseFloat on the result.
parseFloat(dolstr.replace(/[^\d\.]/g, ''))

Categories