Skip to content

Instantly share code, notes, and snippets.

@troZee
Created January 1, 2021 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troZee/ac461dfb3062ff2517d0f017ea1b2df2 to your computer and use it in GitHub Desktop.
Save troZee/ac461dfb3062ff2517d0f017ea1b2df2 to your computer and use it in GitHub Desktop.
Price regex for positive and negative numbers
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}`);
});
}
@troZee
Copy link
Author

troZee commented Jan 1, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment