Guide to Generating and Testing Regular Expressions

Updated 

Regular expressions (regex) are powerful tools for pattern matching and text manipulation. This documentation provides a comprehensive guide to generating regex patterns and testing them effectively.


Use cases:

You can utilize regex in defining conditions for live chat visibility, defining conditional messages, defining proactive prompt conditions, restricting inputs in contact detail form etc.

How to generate regex:

While you can write regex by understanding its rules which you refer from this article and use it for various different use cases in live chat
configuration.

Commonly used conditions and regex:

When you need to restrict visibility of the live chat widget or show conditional message on basis or some parameter in the URL or even trigger different prompts on basis of page URL you can utilise the following conditions:

1. Exact Match for a Parameter:

Condition: The URL should contain a specific parameter with a certain value.

Regex: (\?|&)param=value(&|$)

2. Partial Match for a Parameter Value:

Condition: The URL should contain a parameter with a value that partially matches a specified pattern.

Regex: (\?|&)param=.*partial_value.*(&|$)

3. Path Structure:

Condition: The URL path should follow a specific structure.

Regex: ^https?://example\.com/category/\d+/product/\w+$

4. Presence of a Subdomain:

Condition: The URL should contain a specific subdomain.

Regex: ^(https?://)?subdomain\.example\.com

5. URL Parameter Existence:

Condition: The URL should contain a specific parameter regardless of its value.

Regex: (\?|&)param(&|$)

Similar to page URL, you can put conditions on the email address entered in the contact detail form to validate and filter out email addresses on basis of following conditions:

1. Emails from a Specific Domain:

Condition: Email addresses should be from a specific domain (e.g., example.com).

Regex: ^[a-zA-Z0-9._%+-]+@example\.com$

2. Emails from Any Subdomain of a Domain:

Condition: Email addresses should be from any subdomain of a specific domain (e.g., sub.example.com).

Regex: ^[a-zA-Z0-9._%+-]+@sub\.example\.com$

3. Emails from a List of Allowed Domains:

Condition: Email addresses should be from a list of allowed domains (e.g., example.com, test.org).

Regex: ^[a-zA-Z0-9._%+-]+@(example\.com|test\.org)$

4. Emails with a Specific TLD:

Condition: Email addresses should have a specific top-level domain (TLD) (e.g., .edu).

Regex: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.(edu)$

5. Emails with a Specific Subdomain and Domain:

Condition: Email addresses should have a specific subdomain and domain (e.g., user@sub.example.com).

Regex: ^[a-zA-Z0-9._%+-]+@sub\.example\.com$

There are many times when we want to put some condition of the phone number user enters, it could be because of the country’s phone no digit count or if we want to allow or restrict user from some geographies only.
For such use cases we can utilise the following regex conditions:

1. General Phone Number (No specific format):

Condition: Accept any phone number without enforcing a specific format.

Regex: ^\+?[0-9\s\-]+$

2. Numeric Phone Number (Only digits, no spaces or special characters):

Condition: Accept only numeric phone numbers.

Regex: ^[0-9]+$

3. Specific Country Code (e.g., US):

Condition: Enforce a specific country code (e.g., +1 for the United States).

Regex: ^\+1[0-9\s\-]+$

4. Specific Length Requirement (e.g., 10 digits):

Condition: Enforce a specific length for the phone number (e.g., 10 digits).

Regex: ^\d{10}$

5. Optional Parentheses and Hyphens (Common in the US):

Condition: Accept phone numbers with optional parentheses and hyphens.

Regex: ^\+?1?[-.\s]?(\(\d{3}\)|\d{3})[-.\s]?\d{3}[-.\s]?\d{4}$

6. International Format (With or without country code):

Condition: Accept phone numbers in international format (with or without the country code).

Regex: ^\+?\d+$

In the use cases when we want to define the password or put restrictions on it in the Secure form asset, we can utilise the following regex
conditions and their combinations:

1. Minimum Length (e.g., at least 8 characters):

Condition: Password must be a minimum length.

Regex: ^.{8,}$

2. Uppercase and Lowercase Letters:

Condition: Password must contain both uppercase and lowercase letters.

Regex: ^(?=.*[a-z])(?=.*[A-Z]).*$

3. Numbers:

Condition: Password must contain at least one numeric digit.

Regex: ^(?=.*\d).*$

4. Special Characters:

Condition: Password must contain at least one special character (e.g., !@#$%^&*).

Regex: ^(?=.*[!@#$%^&*]).*$

5. No Spaces:

Condition: Password should not contain any spaces.

Regex: ^\S*$

6. Combination of Characters (Letters, Numbers, and Special Characters):

Condition: Password must contain a combination of letters, numbers, and special characters.

Regex: ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*]).*$

7. No Consecutive Characters:

Condition: Password should not contain consecutive characters (e.g., "abc" or "123").

Regex: ^(?!.*([a-zA-Z0-9])\1).*$

There are numerous use cases in which we want to apply conditions on number type custom field like if it’s amount of order placed, in all such cases we can utilise the following regex conditions and their combinations:

1. Positive Integer:

Condition: Accept positive integers only.

Regex: ^[1-9]\d*$

2. Negative Integer:

Condition: Accept negative integers only.

Regex: ^-[1-9]\d*$

3. Positive or Negative Integer:

Condition: Accept positive or negative integers.

Regex: ^-?[1-9]\d*$

4. Positive Decimal Number:

Condition: Accept positive decimal numbers (with or without leading zero).

Regex: ^(0|[1-9]\d*)(\.\d+)?$

5. Negative Decimal Number:

Condition: Accept negative decimal numbers (with or without leading zero).

Regex: ^-(0|[1-9]\d*)(\.\d+)?$

6. Positive or Negative Decimal Number:

Condition: Accept positive or negative decimal numbers (with or without leading zero)

Regex: ^-?(0|[1-9]\d*)(\.\d+)?$

7. Allow Exponential Notation:

Condition: Accept numbers in exponential notation (e.g., 1.23e4).

Regex: ^-?(0|[1-9]\d*)(\.\d+)?([eE][-+]?\d+)?$

8. Range of Numbers (e.g., 1 to 100):

Condition: Accept numbers within a specific range.

Regex: ^(0*?[1-9]\d?|100)$

How to test regex:

Testing regular expressions (regex) is crucial for accuracy. Utilize online regex testers, like regex101 or RegExr, to input sample text and evaluate the regex pattern's matching performance in real-time.