Regex to Match Percentages
Match percentage values with this regex pattern. Validates numbers followed by a percent sign, including decimals. Free regex tool.
Regular Expression
/^-?\d+(?:\.\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) |
| % | Matches the literal character '%' |
| $ | Anchors at the end of the string (or line in multiline mode) |
Detailed Explanation
This regex matches percentage values in standard notation. Here is the token-by-token breakdown:
^ — Anchors the match at the start of the string to ensure the entire input is a percentage.
-? — Matches an optional minus sign. Negative percentages are valid in many contexts, such as financial losses or negative growth rates.
\d+ — Matches one or more digits for the whole number part. The \d shorthand matches digits 0-9 and the + requires at least one.
(?:.\d+)? — An optional non-capturing group matching a decimal point followed by one or more digits. This allows both whole number percentages like '50%' and decimal percentages like '99.9%'. The entire group is optional.
% — Matches a literal percent sign at the end. This character is not a regex metacharacter, so it does not need escaping.
$ — Anchors the match at the end of the string.
This pattern validates percentage values commonly found in statistics, financial data, CSS properties, and user interfaces. It matches values like '100%', '3.14%', '-5%', and '0.1%'. Note that it does not restrict the range to 0-100, since percentages above 100% (like '150%' for zoom levels) and below 0% are valid in many applications. If you need to restrict the range, you would need additional logic in your application code or a more complex regex with numeric alternation similar to the port number pattern.
Example Test Strings
| Input | Expected |
|---|---|
| 50% | Match |
| 99.9% | Match |
| -5% | Match |
| 50 | No Match |
| %50 | No Match |
Try It — Interactive Tester
18 charsFlags: noneMatches: 0Ctrl+Shift+C to copy regex