Friday, July 28, 2023

/^1?$|^(11+?)\1+$/

Here's a fascinating Regular Expression for identifying prime numbers:

/^1?$|^(11+?)\1+$/


A Regular Expression is a sequence of characters that specifies a match pattern in text. For example, it could be used when writing code for a website to detect if someone entered a properly formatted phone number, ZIP code, etc.

Instead of entering numbers in base 10 or binary (base 2), this Regular Expression uses the unary numeral system (base 1). Base 1 is probably how the earliest humans first counted. 111 = 3, 11111111 = 8, etc.

It seems the more I learn about Regular Expressions, the less I know. 🤷‍♂️

Here's a ten minute explainer video that demos regex101.com for testing Regular Expressions.