Regex to Match Scientific Notation
Validate numbers in scientific or exponential notation with this regex. Matches formats like 1.5e10, -3.14E-5, and 2e3. Free online regex tool.
Regular Expression
/^-?\d+(?:\.\d+)?[eE][+-]?\d+$/
Token Breakdown
| Token | Description |
|---|---|
| ^ | Anchors at the start of the string (or line in multiline mode) |
| - | Matches the literal character '-' |
| ? | Makes the preceding element optional (zero or one times) |
| \d | Matches any digit (0-9) |
| + | Matches the preceding element one or more times (greedy) |
| (?: | Start of non-capturing group |
| \. | Matches a literal dot |
| \d | Matches any digit (0-9) |
| + | Matches the preceding element one or more times (greedy) |
| ) | End of group |
| ? | Makes the preceding element optional (zero or one times) |
| [eE] | Character class — matches any one of: eE |
| [+-] | Character class — matches any one of: +- |
| ? | Makes the preceding element optional (zero or one times) |
| \d | Matches any digit (0-9) |
| + | Matches the preceding element one or more times (greedy) |
| $ | Anchors at the end of the string (or line in multiline mode) |
Detailed Explanation
This regex matches numbers written in scientific notation (also called exponential notation). Here is the token-by-token breakdown:
^ — Anchors the match at the start of the string.
-? — Matches an optional minus sign for negative numbers.
\d+ — Matches one or more digits for the integer part of the coefficient (the number before the exponent). At least one digit is required.
(?:.\d+)? — An optional non-capturing group that matches a decimal point followed by one or more digits. This allows both integer coefficients like '5e3' and decimal coefficients like '5.2e3'. The entire group is optional thanks to the trailing ?.
[eE] — A character class matching either a lowercase 'e' or uppercase 'E'. Both forms are standard in scientific notation. Programming languages typically accept either case.
[+-]? — An optional character class matching a plus or minus sign for the exponent. Positive exponents like 'e+3' and negative exponents like 'e-3' are both valid. When omitted, the exponent is assumed positive.
\d+ — Matches one or more digits for the exponent value.
$ — Anchors the match at the end of the string.
This pattern validates scientific notation as used in most programming languages and scientific calculators. Examples include 6.022e23 (Avogadro's number), 1.6E-19 (electron charge), and -2.5e4. The pattern ensures a properly formed number with a valid exponent.
Example Test Strings
| Input | Expected |
|---|---|
| 1.5e10 | Match |
| -3.14E-5 | Match |
| 2e3 | Match |
| 1.5 | No Match |
| e10 | No Match |
Try It — Interactive Tester
29 charsFlags: noneMatches: 0Ctrl+Shift+C to copy regex