const regex = /^(?=-.*D$|\d.*C$)-?(?=(?:\d,?){1,13}\.)(?:0|[1-9]\d{0,2}(?:,\d{3})*)\.\d{2} [DC]$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?=-.*D$|\\d.*C$)-?(?=(?:\\d,?){1,13}\\.)(?:0|[1-9]\\d{0,2}(?:,\\d{3})*)\\.\\d{2} [DC]$', 'gm')
const str = `-0.48 D
-1.00 D
-2,000.00 D
-100,000.56 D
-100,000.00 D
-1,123,456.43 D
-1,123,456,000,000.43 D
-10,123,456,000,000.43 D
-1,123,456.43 C
-3,000,62.45 D
-03,000.45 D
-.23 D
0.23 C
1.00 C
2,000.00 C
100,000.56 C
100,000.00 C
1,123,456.43 C
1,123,456,000,000.43 C
10,123,456,000,000.43 C
`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions