/^/
MediumNumbers4 min read

Temperature Regex

Validates a temperature value made of a signed number, an optional space and degree symbol, and a unit letter for Celsius, Fahrenheit, or Kelvin.

#temperature#numbers#units#validation#celsius#fahrenheit

Regex Pattern

^-?\d+(\.\d+)?\s?°?[CFK]$

Pattern Breakdown

Hover over a token to see what it does.

^-?\d+(\.\d+)?\s?°?[CFK]$
TokenMeaning
^Anchors the match to the start of the string.
-?An optional leading minus sign for sub-zero temperatures.
\d+(\.\d+)?The numeric magnitude: an integer, or a decimal with a fractional part.
\s?An optional single whitespace character between the number and the unit.
°?An optional degree symbol before the unit letter.
[CFK]The unit: C for Celsius, F for Fahrenheit, or K for Kelvin.
$Anchors the match to the end of the string.

Detailed Explanation

What it does

This pattern matches a temperature reading such as 98.6°F, -40°C, 100C, or 0K. It requires a numeric magnitude (optionally negative and optionally decimal) followed by an optional space, an optional degree symbol, and a required unit letter for Celsius, Fahrenheit, or Kelvin.

Why it works

Making both the space and the degree symbol optional with `\s?` and `°?` lets the pattern accept the many ways people actually write temperatures, from a tightly packed '100C' to a spaced-out '37.5 °C', while still requiring a valid unit letter at the end. The unit character class `[CFK]` keeps the accepted units to exactly the three common temperature scales.

Common use cases

  • Parsing temperature values out of weather API responses or sensor logs
  • Validating a user-entered temperature field in a health or climate tracking app
  • Normalizing free-text temperature mentions before converting between units
  • Filtering scraped text for strings that look like temperature readings

Edge cases

  • -40°C matches even though -40 is the point where Celsius and Fahrenheit scales cross, purely a numeric coincidence here
  • 100C matches with no space or degree symbol, since both are optional
  • 100° fails because a degree symbol alone without a following unit letter doesn't satisfy the required [CFK]
  • 100X fails because X isn't one of the three accepted unit letters

Limitations

  • Does not enforce a physically valid range, such as rejecting temperatures below absolute zero for Kelvin
  • Only recognizes single-letter unit abbreviations, not full words like 'Celsius' or 'Fahrenheit'
  • Assumes at most one space between the number and the unit, not multiple spaces or tabs

Interactive Tester

Edit the pattern or text below — matching runs live in your browser.

98.6°F -40°C 100C

Test Cases

Editable — add your own inputs to see if they pass.

InputExpectedResult
Pass
Pass
Pass
Pass
Pass
Pass
Pass
Pass

Language Variants

Production-ready examples in 12 languages.

const temperaturePattern = /^-?\d+(\.\d+)?\s?°?[CFK]$/;

function isTemperature(value) {
  return temperaturePattern.test(value);
}

console.log(isTemperature("98.6°F")); // true

Common Mistakes

Requiring the degree symbol, which rejects common plain forms like 100C that omit it.

Fix: Make the degree symbol optional with `°?` so both '100°C' and '100C' are accepted.

Allowing any letter as the unit instead of restricting to C, F, and K.

Fix: Use the explicit character class `[CFK]` so unrelated letters like X don't match.

Not accounting for the degree symbol being a multi-byte UTF-8 character in some languages.

Fix: Ensure the regex engine and source file encoding are UTF-8 aware, and add a Unicode flag (like PHP's `u` modifier) where required.

Performance Notes

  • All quantifiers are bounded and non-overlapping, keeping the match in linear time with no backtracking blowup risk.
  • Anchoring both ends lets clearly invalid strings fail immediately.
  • The optional space and degree-symbol groups add negligible overhead since each is checked at most once.

Browser Compatibility

EngineSupportedNotes
ChromeYes
FirefoxYes
SafariYes
EdgeYes
Node.jsYes