const regex = /((?:[+-])?(?:(?:0?\.[0-9]*)|(?:[1-9]+\.?[0-9]*))(?:[eE][+-]?(?:(?:0?\.[0-9]*)|(?:[0-9]+\.?[0-9]*)))?)|(0x[0-9a-fA-F]+)|(0[0-9]*)/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('((?:[+-])?(?:(?:0?\\.[0-9]*)|(?:[1-9]+\\.?[0-9]*))(?:[eE][+-]?(?:(?:0?\\.[0-9]*)|(?:[0-9]+\\.?[0-9]*)))?)|(0x[0-9a-fA-F]+)|(0[0-9]*)', 'g')
const str = `Matching Groups:
===============================
Group 0x1: hexadecimal numbers
Group 02: octal numbers
Group 3: decimal numbers
1, 04, 3., +1.2e4, -0.03E-4, -.456E+003, - 04, .e4, 1.3e
-0.04e-03 0x024 0 0x 0xG 0xF 0xf 0x3.4 0123345
-00 -0xf.3e0f 0xFF00233 e09 e0xk22`;
// 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