Products

Solutions

Resources

Search for API Security Tests

Javascript RegEx Tester

Javascript RegEx Tester

Regex Tester Online tools to learn, build & test Regular Expression (RegEx/RegExp)
0 match
/
/ gm
Possible security issues
Explanation
Match information
Akto.io

Regular Expression - Documentation

Regular Expression - Documentation

Introduction

JavaScript regular expressions are a powerful tool for pattern matching and text manipulation. Utilizing the built-in capabilities of JavaScript, developers can perform complex operations on strings, such as validation, parsing, searching, and replacing.

Core Constructs of JavaScript Regex

JavaScript regex operates with various constructs, each serving specific purposes:

Metacharacters

  • .: Matches any single character, except newline characters.

  • ^: Asserts the start of a line or string.

  • $: Asserts the end of a line or string.

  • |: Acts as a logical OR operator.

Character Classes

  • [abc]: Matches any one of the characters a, b, or c.

  • [^abc]: Negates the set; matches any character except a, b, or c.

  • [a-zA-Z]: Specifies a range, matching any letter from a to z or A to Z.

Predefined Character Classes

  • \\d: Matches any digit, equivalent to [0-9].

  • \\D: Matches any non-digit.

  • \\s: Matches any whitespace character.

  • \\S: Matches any non-whitespace character.

  • \\w: Matches any word character (alphanumeric & underscore).

  • \\W: Matches any non-word character.

Quantifiers

  • ``: Zero or more times.

  • +: One or more times.

  • ?: Zero or one time; also used to denote a non-greedy quantifier.

  • {n}: Exactly n times.

  • {n,}: n or more times.

  • {n,m}: Between n and m times, inclusive.

Special Constructs

  • (abc): A capturing group that matches the sequence abc.

  • (?:abc): A non-capturing group.

  • (?=abc): A positive lookahead, asserts that what follows is abc.

  • (?!abc): A negative lookahead, asserts that what follows is not abc.

Anchors and Boundaries

  • \\b: Matches a word boundary.

  • \\B: Matches a non-word boundary.

  • \\A: Matches the beginning of the input.

  • \\Z: Matches the end of the input, before a final newline (if any).

Flags

  • i: Case-insensitive matching.

  • g: Global search.

  • m: Multi-line search.

  • u: Unicode; treat a pattern as a sequence of Unicode code points.

  • y: Sticky; matches only from the index indicated by the lastIndex property of this regular expression in the target string.

JavaScript Regular Expressions Examples

Example 1: Email Validation


const emailPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\\\.[a-zA-Z]{2,}$/;
const email = "user@example.com";
console.log("Email Valid:", emailPattern.test(email));

Example 2: Password Strength Check


const passwordPattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
const password = "Aa123456!";
console.log("Password Strong:", passwordPattern.test(password));

Example 3: Extracting Words from a String


const text = "Regex is #1 at pattern matching!";
const wordPattern = /\b\w+\b/g;
let match;
while ((match = wordPattern.exec(text)) !== null) {
    console.log("Found:", match[0]);
}

Practical Tips for JavaScript Regular Expressions

  1. Familiarize yourself with the different flags and how they alter the behavior of your regex.

  2. Use the RegExp constructor when you need dynamic patterns, but be mindful of the need to escape backslashes.

  3. Regular expressions in JavaScript are stateful when used with global or sticky flags. Be aware of the lastIndex property.

  4. Test and debug your regular expressions thoroughly. Tools like regex101 or Akto regex tester can be invaluable for this.

  5. For complex patterns, consider breaking them down into smaller parts or using verbose mode (comments and whitespace in pattern) if using a transpiler or build tool that supports it.

  6. Remember that JavaScript regex is capable of handling Unicode characters but requires the u flag for full Unicode support.

  7. Use non-capturing groups ((?:...)) when you don't need to save the captured match.

  8. Practice writing regular expressions for common tasks like email and URL validation, but also explore ready-made solutions for more reliable and maintainable code.

  9. Be mindful of potential performance issues in large-scale applications, as complex regex can be computationally expensive.

  10. Leverage modern JavaScript features like template literals for more readable and maintainable regex patterns.

These tips and examples should provide a solid foundation for working with regular expressions in JavaScript. Remember to utilize tools like Akto's regex tester for efficient debugging and validation of your regex patterns.

Frequently asked questions

What is the JavaScript Regex Tester?

How do I create a basic regex pattern for text matching in JavaScript?

Can I use flags with regex patterns in JavaScript?

What is the difference between RegExp and regex literal notation in JavaScript?

How do I test a regex pattern using the JavaScript Regex Tester?

Javascript RegEx Tester

/

/

Javascript RegEx Tester