Skip to content

Instantly share code, notes, and snippets.

@nklatt
Last active February 6, 2024 16:22
Show Gist options
  • Save nklatt/1457f0835fb8c8e84aeb58837a5bdc8c to your computer and use it in GitHub Desktop.
Save nklatt/1457f0835fb8c8e84aeb58837a5bdc8c to your computer and use it in GitHub Desktop.
HTML/JS regex pattern to match one of: whole number, float, fraction or whole number <space> fraction
<input pattern="/^(?:(\d+\/[1-9]\d*)|(\d*(?:(\s\d+\/[1-9]\d*)|\.\d+)?))$/">
<!--
(?: => non-capturing group, EITHER
(\d+\/[1-9]\d*) => any number of digits followed by a slash then any number of digits not beginning with 0 (fraction)
| => OR
(\d*(?: => zero or more digits followed by non-capturing group, EITHER
(
\s => a space
\d+ => any number of digits
\/ => a slash
[1-9]\d* => one or more digits not beginning with 0
) => (fraction OR whole + fraction)
| => OR
\.\d+ => a decimal point followed by any number of digits (float)
)?) => zero or one match for that non-capturing group
)
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment