JavaScript / ECMAScript

Percentage in JS

Matches percentage values with optional decimal and a trailing % sign.

Try it in the JS tester →

Pattern

regexJS
^\d+(\.\d+)?%$

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("^\\d+(\\.\\d+)?%$", "");
const input = "95%";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern works

`^\d+` matches integer part. `(\.\d+)?` optional decimals. `%$` requires a trailing percent sign at end.

Examples

Input

95%

Matches

  • 95%

Input

99.9%

Matches

  • 99.9%

Input

100

No match

Same pattern, other engines

← Back to Percentage overview (all engines)