const regex = /^(?:[1-9][0-9]*?,?[0-9]{1,2}|[0](?:,[0-9]{1,2})?|)$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?:[1-9][0-9]*?,?[0-9]{1,2}|[0](?:,[0-9]{1,2})?|)$', 'gm')
const str = `Input amount is not a string (only number 0-9)
Adwad,321 d3,232,123,A;d,aw
123456789
Separator must be ","
12.1
12,1
I need max 2 decimal places
17,234236
17,23
I can't accept any characters, except of numbers 0-9 and ","
321d,3e16
-323,3
123,45
Input amount cannot be less than 0
-1
0
0,01
Input amount has to start from a number
a31233
,321
1232
extra; does not allow "fake/mal-formatted" numbers
0312312
032312
extra 2; doesnt last character to be ","
1,0
1,
0,12
0,
3123,`;
// 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