Skip to content

Instantly share code, notes, and snippets.

@mhgarry
Last active September 1, 2024 00:59
Show Gist options
  • Save mhgarry/1931baca8a4bbf805ef235eff094405d to your computer and use it in GitHub Desktop.
Save mhgarry/1931baca8a4bbf805ef235eff094405d to your computer and use it in GitHub Desktop.
Let's Learn Some Regex!

Fortify Your Web Application: Building a Strong Password Validation Regex for User Security

Summary

Regular expressions (regex) are an excellent tool for implementing robust verification and security measures in web applications involving user input or sensitive data. Specifically, regex can be used to search for patterns in strings, determining whether they match the desired format. This is particularly valuable when registering a user in a web application's database to ensure they meet the required security standards.

In this example, the regex will validate that a string is not empty, contains a lowercase letter, an uppercase letter, a number, and a special character. Additionally, it ensures that the string is between 8 and 32 characters in length. If the string matches these criteria, the user is registered in the web application. Otherwise, the user will be prompted to create a password that meets the requirements.

There are several reasons why this is useful for user registration in a web application. These include user protection, user authentication, and database security. The strict requirements of the regex make it difficult for malicious actors to access user account information and database data using techniques such as code injection and SQL injection. Below is an example of our regex expression.

Regex Requirements Example:
The string must include at least one lowercase character, one uppercase character, one number, and one special character. The string must be between 8-32 characters long.

JavaScript Regex Example:
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%?&])[A-Za-z\d@$!%?&]{8,32}$/i


Table of Contents

  • Anchors
  • Quantifiers
  • OR Operator
  • Character Classes
  • Flags
  • Grouping and Capturing
  • Bracket Expressions
  • Greedy and Lazy Match
  • Boundaries
  • Back-references
  • Look-ahead and Look-behind

Regex Breakdown

Our regex /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%?&])[A-Za-z\d@$!%?&]{8,32}$/i validates the criteria described in the summary. Below is a detailed breakdown of its components.

  • .*[a-z]: Matches any string containing at least one lowercase letter (a to z).

    • . matches any single character, while * allows repetition of this pattern.
    • [a-z] specifies that a lowercase letter must be present.
  • .*[A-Z]: Similar to the previous component but matches an uppercase letter (A to Z).

  • .*\d: Ensures that the string contains a number (0-9).

  • .*[@$!%?&]: Matches at least one special character from the specified set (@$!%?&).

  • [A-Za-z\d@$!%?&]{8,32}: Confirms that the string is composed only of the allowed characters (letters, digits, special characters) and is between 8 and 32 characters long.


Detailed Explanation

Anchors

Anchors in regex match specific positions within a string.

  • ^: Indicates the start of the string.
  • $: Indicates the end of the string.

These ensure that the entire string matches the pattern, from beginning to end.

Quantifiers

Quantifiers specify how many times a character or group of characters can appear.

  • *: Matches zero or more occurrences.
  • ?: Matches zero or one occurrence.
  • {}: Specifies the exact number or range of allowed occurrences. In our example, {8,32} enforces a minimum of 8 and a maximum of 32 characters.

OR Operator

Our regex does not include an OR (|) operator, as it's not relevant for this particular validation.

Character Classes

Character classes define sets of characters to be matched.

  • .: Matches any character except newline (\n).
  • \d: Matches any digit (0-9).
  • [A-Za-z\d@$!%?&]: Matches any uppercase letter, lowercase letter, digit, or special character.

Flags

Flags modify the behavior of a regex.

  • i: Makes the pattern case-insensitive, so both uppercase and lowercase characters are treated equally.

Grouping and Capturing

Grouping allows us to treat multiple characters as a single unit.

  • (a-z), (A-Z), (\d), and (@$!%?&): These capture groups define the sets of characters allowed in the input string.

Bracket Expressions

Bracket expressions, like [a-z] and [A-Z], define the specific characters allowed within the group.

Greedy and Lazy Matching

  • Greedy matching (e.g., *) attempts to match as many characters as possible. In our regex, this ensures that the input string contains at least one of each required character.

Boundaries

Boundaries define the limits of the string.

  • ^ and $ anchor the beginning and end of the string, ensuring that the entire string meets the criteria.

Back-references

No back-references are used in this regex.

Look-ahead and Look-behind

Look-aheads and look-behinds are also not used in this regex.


Author

I'm Matthew Garry, a MERN stack web developer passionate about creating fast, functional, and aesthetically pleasing web applications. My projects are designed to work seamlessly across platforms, including Windows, Linux, macOS, Android, and iOS. Check out my work on GitHub.


Sources


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment