Created
January 1, 2021 13:10
-
-
Save troZee/ac461dfb3062ff2517d0f017ea1b2df2 to your computer and use it in GitHub Desktop.
Price regex for positive and negative numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const regex = /(^\-?0[\,\.][0-9]{1,}$)|(^\-?[1-9][0-9]{1,}$)|(^[0-9]$)|(^\-?[1-9][0-9]{0,}[\,\.][0-9]{1,}$)|(^\-?[1-9]$)/gm; | |
const str = `01 | |
001 | |
033 | |
0000033 | |
00000.2 | |
00000000000000 | |
01010101010 | |
0000.0 | |
aaa | |
???!!!!! | |
111%\$\$\$\$ | |
-0 | |
- | |
-100 | |
-100.255 | |
-10.25555 | |
-1.2 | |
-1335354.12 | |
-22 | |
-2 | |
-1 | |
-13353544366564547 | |
-0.22222298 | |
-0.2 | |
-0,2989080 | |
-6767676767676767677676767677676 | |
-0.22 | |
-123 | |
100.255 | |
10.25555 | |
1.2 | |
1335354.12 | |
22 | |
2 | |
1 | |
13353544366564547 | |
0.22222298 | |
0.2 | |
0,2989080 | |
6767676767676767677676767677676 | |
0 | |
0.22 | |
123`; | |
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}`); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://regex101.com/r/RgxEpr/2/