JSON Number (Strict)
Match JSON-spec numbers — disallows leading zeros (no `01`), allows decimals and exponents.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][-+]?\\d+)?", "g");
const input = "values: 0, 42, -3.14, 6.022e23";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][-+]?\d+)?")
input_text = "values: 0, 42, -3.14, 6.022e23"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][-+]?\d+)?`)
input := `values: 0, 42, -3.14, 6.022e23`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][-+]?\d+)? (flags: g)Raw source: -?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][-+]?\d+)?
How it works
Examples
Input
values: 0, 42, -3.14, 6.022e23Matches
042-3.146.022e23
Input
0.5 vs invalid 01Matches
0.501
Input
no numbersNo match
—Common use cases
- •JSON validators and lexer fallback paths
- •Number-extraction from semi-JSON formats
- •Detecting JS-isms in JSON payloads
- •Quick parsing in observability pipelines
Related patterns
JSON Key-Value Pair (Simple)
Text ProcessingExtract simple `"key": value` pairs from JSON-ish text (strings, numbers, booleans, null).
JSON Boolean / Null Literal
Text ProcessingMatch JSON `true`, `false`, and `null` literal values, with word boundaries to avoid partial matches.
Negative Lookbehind (Decimals Without $)
Text ProcessingUse negative lookbehind `(?<!...)` to match decimal numbers NOT preceded by a dollar sign.
Whitespace Trim (Leading & Trailing)
Text ProcessingMatch leading and/or trailing whitespace on a string — the regex equivalent of .trim().