Percentage Regex
Validates a percentage value: a signed integer or decimal number immediately followed by a trailing percent sign.
Regex Pattern
^-?\d+(\.\d+)?%$Pattern Breakdown
Hover over a token to see what it does.
| Token | Meaning |
|---|---|
| ^ | Anchors the match to the start of the string. |
| -? | An optional leading minus sign for negative percentages. |
| \d+ | One or more digits for the integer part of the value. |
| (\.\d+)? | An optional decimal fraction, a dot followed by one or more digits. |
| % | A required literal percent sign. |
| $ | Anchors the match to the end of the string. |
Detailed Explanation
What it does
This pattern matches a number, optionally negative and optionally decimal, that is immediately followed by a percent sign, such as 50%, 3.14%, or -12.5%. There is no space allowed between the number and the percent sign, and the percent sign is required.
Why it works
The numeric portion reuses the common `\d+(\.\d+)?` shape for an integer-or-decimal magnitude, and the literal `%` at the end (which needs no escaping since it isn't a regex metacharacter) makes the percent sign mandatory. Anchoring both ends ensures a bare number without a percent sign, or a percent sign with extra trailing characters, is rejected.
Common use cases
- Validating a discount, tax rate, or progress field in a form
- Parsing percentage values out of scraped or generated report text
- Pre-checking spreadsheet or CSV cells formatted as percentages before conversion to a decimal ratio
- Filtering log or config values that represent thresholds as percentages
Edge cases
- 0% and 100% both match since the pattern places no bound on the magnitude of the number
- -12.5% matches, allowing negative percentages such as a decline in a metric
- 50 (no percent sign) fails, since the percent sign is mandatory rather than optional
- 5.5.5% fails because the anchors force the whole string to be a single valid number followed by one percent sign
Limitations
- Does not enforce a 0-100 range, so values like 500% or -300% are accepted as valid shape
- Does not allow a space between the number and the percent sign, which some locales use
- Does not support alternative percent notations like the word 'percent' spelled out
Interactive Tester
Edit the pattern or text below — matching runs live in your browser.
Test Cases
Editable — add your own inputs to see if they pass.
| Input | Expected | Result | |
|---|---|---|---|
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass |
Language Variants
Production-ready examples in 12 languages.
const percentagePattern = /^-?\d+(\.\d+)?%$/;
function isPercentage(value) {
return percentagePattern.test(value);
}
console.log(isPercentage("3.14%")); // trueCommon Mistakes
Forgetting the trailing `%` is required, so a plain number incorrectly passes validation.
Fix: Keep `%` as a mandatory literal at the end of the pattern rather than making it optional.
Assuming this pattern enforces a sane 0-100 range for the percentage value.
Fix: After the regex passes, parse the numeric part and apply an explicit range check if the use case requires it.
Not accounting for a space between the number and percent sign used in some locales, like '50 %'.
Fix: Add an optional `\s?` before the `%` if spaced percentages should also be accepted.
Performance Notes
- The numeric group and trailing literal are non-overlapping, so matching runs in linear time with no backtracking risk.
- Anchoring both ends lets the engine reject non-percentage strings immediately.
- Precompiling the regex once and reusing it avoids repeated compilation overhead in hot code paths.
Browser Compatibility
| Engine | Supported | Notes |
|---|---|---|
| Chrome | Yes | — |
| Firefox | Yes | — |
| Safari | Yes | — |
| Edge | Yes | — |
| Node.js | Yes | — |