Skip to content

Instantly share code, notes, and snippets.

@yitonghe00
Created September 12, 2021 01:39
Show Gist options
  • Save yitonghe00/2787adcaa07852e861f21cba1440baac to your computer and use it in GitHub Desktop.
Save yitonghe00/2787adcaa07852e861f21cba1440baac to your computer and use it in GitHub Desktop.
// Fill in this regular expression.
let number = /^[+-]?(\d+\.?\d*|\.\d+)(E[+-]?\d+)?$/i;
console.log(/\d/.test("."));
// Tests:
for (let str of ["1", "-1", "+15", "1.55", ".5", "5.",
"1.3e2", "1E-4", "1e+12"]) {
if (!number.test(str)) {
console.log(`Failed to match '${str}'`);
}
}
for (let str of ["1a", "+-1", "1.2.3", "1+1", "1e4.5",
".5.", "1f5", "."]) {
if (number.test(str)) {
console.log(`Incorrectly accepted '${str}'`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment