Javascript regular expression for currency format
You could perhaps use:
^(?!0\.00)\d{1,3}(,\d{3})*(\.\d\d)?$
See how it's working here.
Additionally, if you'd like to forbid leading zeros the regex would be:
^(?!0\.00)[1-9]\d{0,2}(,\d{3})*(\.\d\d)?$
Javascript regex: format money
You can use this simple function to format your decimal numbers:
function fmt(num) {
// split into two; integer and fraction part
var arr = num.match(/^(\d+)((?:\.\d+)?)$/);
// format integer part and append fraction part
return arr[1].replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') + arr[2];
}
var s1 = fmt('1130.000200')
//=> "1,130.000200"
var s2 = fmt('1130000200')
//=> "1,130,000,200"
What is "The Best" U.S. Currency RegEx?
here's some stuff from the makers of Regex Buddy. These came from the library so i'm confident they have been thoroughly tested.
Number: Currency amount (cents mandatory)
Optional thousands separators; mandatory two-digit fraction
Match; JGsoft:
^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*\.[0-9]{2}$
Number: Currency amount (cents optional)
Optional thousands separators; optional two-digit fraction
Match; JGsoft:
^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?$
Number: Currency amount US & EU (cents optional)
Can use US-style 123,456.78 notation and European-style 123.456,78 notation. Optional thousands separators; optional two-digit fraction
Match; JGsoft:
^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{2})?|(?:,[0-9]{3})*(?:\.[0-9]{2})?|(?:\.[0-9]{3})*(?:,[0-9]{2})?)$
Regular Expression for Currency
If you want to disallow 0.00
value, and allow numbers without a digit grouping symbol, you can use
/^(?!0+\.0+$)\d{1,3}(?:,\d{3})*\.\d{2}$/.test(your_str)
See the regex demo
Explanation:
^
- start of string(?!0+\.0+$)
- negative lookahead that fails the match if the input is zero\d{1,3}
- 1 to 3 digits(?:,\d{3})*
- 0+ sequences of a comma followed with 3 digits\.
- a literal dot\d{2}
- 2 digits (decimal part)$
- end of string.
document.body.innerHTML = /^(?!0+\.0+$)\d{1,3}(?:,\d{3}|\d)*\.\d{2}$/.test("1,150.25");document.body.innerHTML += "<br/>" + /^(?!0+\.0+$)\d{1,3}(?:,\d{3}|\d)*\.\d{2}$/.test("0.25");
document.body.innerHTML += "<br/>" + /^(?!0+\.0+$)\d{1,3}(?:,\d{3})*\.\d{2}$/.test("25");document.body.innerHTML += "<br/>" + /^(?!0+\.0+$)\d{1,3}(?:,\d{3})*\.\d{2}$/.test("0.00");document.body.innerHTML += "<br/>" + /^(?!0+\.0+$)\d{1,3}(?:,\d{3})*\.\d{2}$/.test("1150.25");
Format input to currency format using regex
You can use
const numbers = ['443gr%%g4', 'gg443.4', '443,4', '4.4,3,12344'];
numbers.forEach(x=>
console.log(
x.replace(/[^\d.,]+/g,'').replace(/[.,](?![^,.]*$)/g, '').replace(',','.')
.replace(/(\.\d{2})\d*$/, '$1')
.replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1 ')
)
);
Javascript regex for currency format
Edit : The correct answer is in the edit part, below
Here is something that works for every case I could think about :
Replace :(\d+)(?:(?:[\.\s]0+|(\.\d+)))?\s?USD
by$\1\2
However, if there is a missing point between in a non integer number, it doesn't work. For instance, 123 456
USD will be replaced by 123 $456
Also, I couldn't find a way to let numbers like "123.456 USD" untouched (see comments below)
Demo here
I can provide more details if you need any explanation.
If you see some cases I didn't handle, just say it !
Code for Javascript :
var str = '123.00 USD 1234 USD 357.0 USD 456 00 USD 651USD 753.684 USD 123 456 USD -123 USD 789.00 XXX 123.00 wrongcurrency test string $159';str = str.replace(/(\d+)(?:(?:[\.\s]0+|(\.\d+)))?\s?USD/g, '$$$1$2');console.log(str);
Regex for currency formatting - java
Your pattern could match repeating dots or repeating comma's only because all the parts are optional due to the question mark. It could also match an empty string.
You could use an alternation with a repeating group that starts with a dot or comma followed by 3 or 2 digits to prevent consecutive dots and commas:
Explanation
^(?:(?![,0-9]{14})\d{1,3}(?:,\d{3})*(?:\.\d{1,2})?|(?![.0-9]{14})\d{1,3}(?:\.\d{3})*(?:\,\d{1,2})?)$
^
Start of string(?:
Non capturing group(?![,0-9]{14})
Negative lookahead, assert not repeating 14 times a comma or digit\d{1,3}(?:,\d{3})*(?:\.\d{1,2})?
Match 1-3 digits, repeat 0+ times matching a comma followed by 3 digits, optionally match a dot and 1-2 digits|
Or(?![.0-9]{14})
Negative lookahead, assert not repeating 12 times a dot or digit\d{1,3}(?:\.\d{3})*(?:\,\d{1,2})?
Match 1-3 digits, repeat 0+ times matching a dot followed by 3 digit, optionally match a comma and 1-2 digits
)
Close non capturing group$
Assert end of string
Regex demo
Currency Regular Expression
You can express "between one and six digits; comma before the last three digits is optional" a bit more tersely as \d{1,3}(,?\d{3})?
. This also allows you to include only two copies of (\.\d{1,2})?
: one for positive and one for negative, instead of one for positive-without-comma, one for positive-with-comma, etc.
Also, \d{1,2}
can be shortened slightly to \d\d?
, though I'm not sure if that's an improvement.
So, barring some notation like (?(1))
to test if a backreference is set, here's the shortest version I see:
^(\$?\d{1,3}(,?\d{3})?(\.\d\d?)?|\(\$?\d{1,3}(,?\d{3})?(\.\d\d?)?\))$
One perhaps-undesirable aspect of your regex, and of this one, is that they will allow something like $00,012.7
, even though no one uses leading zeroes that way. You can address that by requiring the first digit to be nonzero, and then adding a special case to handle $0
and (0.12)
and so on:
^(\$?(0|[1-9]\d{0,2}(,?\d{3})?)(\.\d\d?)?|\(\$?(0|[1-9]\d{0,2}(,?\d{3})?)(\.\d\d?)?\))$
Edited to add: using a lookahead assertion like F.J suggests in his/her answer, the latter can be shortened to:
^(?!\(.*[^)]$|[^(].*\)$)\(?\$?(0|[1-9]\d{0,2}(,?\d{3})?)(\.\d\d?)?\)?$
Related Topics
Create a C# Method to Generate Auto Increment Id
Cannot Access a Closed Stream of a Memorystream, How to Reopen
Validate Indian Phone Number With Optional +91 or 0 Preceeding 10 Digits
Check If Combobox Value Is Empty
How to Open the Default Chrome Profile Through Selenium, Chromedriver and Googlechrome
How to Disable Cursor in Textbox
Algorithm to Detect Overlapping Periods
Reading Large Text Files With Streams in C#
Where Are the Properties.Settings.Default Stored
How to Close a File That Is Already Opened in a Directory
How to Encrypt a Password Within Appsettings.Json for ASP.NET Core 2
Reference External Dll in .Net Core Project
C# How to Simply Encrypt a Text File With a Pgp Public Key
The Find Element Returns Empty String..Using Xpath Contains,Text()