Regex for number

/-?\b\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\b/g

Options
Matches

Examples

Note

Integers, decimals and scientific notation. Thousands separators like 1,000 are two numbers here.

Matches in the example

  • "42"
  • "-7"
  • "3.14"
  • "6.02e23"
  • "-1.5E-3"

Token by token

  1. -? the character “-”, optionally
  2. \b a word boundary
  3. \d+ a digit (0–9), one or more times, as many as possible
  4. (?: a group, not capturing
  5. \. the character “.”
  6. \d+ a digit (0–9), one or more times, as many as possible
  7. )? end of group, whole group optionally
  8. (?: a group, not capturing
  9. [eE] one of: “e” or “E”
  10. [+-]? one of: “+” or “-”, optionally
  11. \d+ a digit (0–9), one or more times, as many as possible
  12. )? end of group, whole group optionally
  13. \b a word boundary

Other patterns