npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fz-regex-generator

v1.0.1

Published

A utility for generating regular expressions based on user input.

Downloads

20

Readme

RegexGenerator

RegexGenerator is a JavaScript class that helps in constructing various regular expressions for validation purposes. It provides methods to build regex patterns dynamically with options for digits, characters, ranges, repetitions, and more.

Table of Contents

Features

  • Digit Patterns: Add patterns for digits with specific counts and optional settings.
  • Character Patterns: Add specific characters or character ranges.
  • Custom Text: Include custom text in the regex.
  • Repetitions: Add patterns for repeated sequences.
  • Flags: Set regex flags for case-insensitivity, global matching, and more.
  • Groups: Include capturing and non-capturing groups, as well as lookaheads and lookbehinds.
  • Static Methods: Predefined regex generators for email, numbers, alphabets, dates, URLs, phone numbers, UUIDs, credit cards, and IP addresses.
  • Persian Credit Card Validation: Validate Persian credit card numbers using a specific algorithm.

Installation

You can install RegexGenerator via npm:

npm install fz-regex-generator

Usage

Here's a quick overview of how to use RegexGenerator:

Creating a Regex Pattern

const RegexGenerator = require('fz-regex-generator');

const reg = new RegexGenerator();
reg.addDigit({ count: 3 })
   .addChar({ value: '-' })
   .addDigit({ count: 2, optional: true })
   .setFlags({ caseInsensitive: true });

const regex = reg.build(); // Builds regex: /\\d{3}-\\d{2}?/i

Using Static Methods

const emailRegex = RegexGenerator.email();
const urlRegex = RegexGenerator.url();
const isValid = RegexGenerator.validatePersianCreditCard('6104338978668818');

Methods

addDigit({ count, optional })

Adds a digit pattern to the regex.

addChar({ value, optional })

Adds a character pattern to the regex.

addRange({ from, to, optional })

Adds a character range to the regex.

addText(text)

Adds custom text to the regex.

addRepeat({ value, min, max })

Adds a repetition pattern to the regex.

setFlags({ caseInsensitive, global, multiline, unicode, sticky })

Sets flags for the regex.

addCapturingGroup(pattern, optional)

Adds a capturing group to the regex.

addNonCapturingGroup(pattern, optional)

Adds a non-capturing group to the regex.

addLookahead(pattern)

Adds a lookahead assertion to the regex.

addNegativeLookahead(pattern)

Adds a negative lookahead assertion to the regex.

addLookbehind(pattern)

Adds a lookbehind assertion to the regex.

addNegativeLookbehind(pattern)

Adds a negative lookbehind assertion to the regex.

build()

Builds and returns the final regex.

validatePattern()

Validates the constructed regex pattern.

Regex Flags

The RegexGenerator class allows you to customize regex flags using the setFlags method. Here’s a summary of the available flags:

  • i: Case-insensitive matching.
  • g: Global matching.
  • m: Multiline mode.
  • u: Unicode matching.
  • y: Sticky mode.

Example Usage

Here’s how you can use these flags in the setFlags method of the RegexGenerator class:

const reg = new RegexGenerator();
reg.setFlags({ caseInsensitive: true, global: true });
// This sets the regex to be case-insensitive and global.

Each flag modifies how the regex engine performs matching, allowing for flexible and powerful pattern matching. For more detailed information, refer to the FLAGS file in this repository.

Static Methods

  • email(): Generates a regex for validating email addresses.
  • wholeNumbers(): Generates a regex for validating whole numbers.
  • decimalNumber(): Generates a regex for validating decimal numbers.
  • alphabets(): Generates a regex for validating alphabets.
  • date(format): Generates a regex for validating dates in a specified format.
  • anyDate(): Generates a regex for validating dates in common formats.
  • compose(parts): Composes multiple regex parts into a single pattern.
  • escapeSpecialChars(str): Escapes special characters in a string.
  • url(): Generates a regex for validating URLs.
  • phoneNumber(): Generates a regex for validating phone numbers.
  • uuid(): Generates a regex for validating UUIDs.
  • creditCard(): Generates a regex for validating credit card numbers.
  • ipAddress(): Generates a regex for validating IP addresses.
  • validatePersianCreditCard(cardNumber): Validates a Persian credit card number.

Contributing

We welcome contributions to RegexGenerator. To contribute:

  1. Fork the repository.
  2. Create a feature branch (git checkout -b feature-branch).
  3. Commit your changes (git commit -am 'Add new feature').
  4. Push to the branch (git push origin feature-branch).
  5. Create a new Pull Request.

Please ensure your code adheres to our coding standards and includes tests where applicable.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contact

For questions or support, please contact salmanfz1681.

FAQ

Q: How do I use the addDigit method?

A: Use addDigit({ count, optional }) to add a digit pattern to your regex. For example, addDigit({ count: 3 }) adds a pattern for exactly 3 digits.

Q: Can I combine multiple patterns?

A: Yes, you can use methods like addText, addRange, and addRepeat to combine various patterns into a single regex.