DEV Community

Sailing
Sailing

Posted on

๐Ÿ’ก โ€œStop Copying Regex!โ€ โ€” Manage, Validate, and Extract All Your Regex with regex-center

Its biggest advantage can be summed up in three words: flexible, flexible, and flexible.

In daily development, regular expressions are everywhere โ€” validating emails, phone numbers, URLs, ID cards, password strength, and more.
But every project and every file seems to have its own version of the same regex. Itโ€™s hard to maintain and easy to mess up.

For example:

const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,4}$/;
if (!emailRegex.test(email)) {
  console.error('Invalid email format');
}
Enter fullscreen mode Exit fullscreen mode

Or like this ๐Ÿ‘‡

if (!/^1[3-9]\d{9}$/.test(phone)) {
  alert('Invalid phone number');
}
Enter fullscreen mode Exit fullscreen mode

Over time, the same regex appears in multiple forms across the codebase โ€”
updating one means a global replace, and team standards become inconsistent.

Eventually, everyoneโ€™s asking the same question:
โ€œWhere did this regex even come from?โ€

Thatโ€™s exactly what regex-center is built to solve.


Regex Center = Regex + Management

regex-center is a professional regex management library โ€”
it comes with 100+ built-in patterns, supports custom extensions, team-wide management, type hints, and ReDoS-safe validation.

Itโ€™s not just for matching โ€” you can also extract, replace, highlight, and mask text easily.

๐Ÿ”ง Core Features

  • โœ… 100+ built-in common regex rules
  • โœ… Named groups (e.g. email:strict, password:strong)
  • โœ… Unified management and extensibility (inject, use)
  • โœ… Full TypeScript support
  • โœ… Built-in ReDoS protection
  • โœ… Advanced text operations (extract, replace, highlight, count)
  • โœ… Works in both browser and Node.js โ€” zero dependencies

๐Ÿš€ Quick Start

Installation

npm install regex-center
Enter fullscreen mode Exit fullscreen mode

Option 1: Use built-in regex rules (plug and play)

// 100+ built-in rules covering all common cases
import { rx } from 'regex-center';

// Retrieve and validate
rx.get('email');                          // โ†’ /^[^\s@]+@[^\s@]+\.[^\s@]+$/
rx.get('phone:CN');                       // โ†’ /^1[3-9]\d{9}$/
rx.test('email', 'user@example.com');     // true
rx.test('phone:CN', '13800138000');       // true
rx.test(/^[^\s@]+@[^\s@]+\.[^\s@]+$/, 'user@company.com'); // true

// Grouped rules
rx.test('password:strong', 'Password123!');      // strong password
rx.test('email:enterprise', 'user@company.com'); // enterprise email

// Text processing
const text = 'Contact: user@example.com, Phone: 13800138000';
rx.extractAll('phone:CN', text);             // extract phone numbers
rx.replaceAll('phone:CN', text, '[PHONE]');  // replace phone numbers
rx.highlight('phone:CN', text, '<mark>$&</mark>'); // highlight
Enter fullscreen mode Exit fullscreen mode

Option 2: Build a team-wide regex management system (recommended)

// Replace built-in regexes with your own team standards
rx.use({
  email: /^[a-z0-9._%+-]+@company\.com$/,  // only allow company emails
  phone: {
    default: 'mobile',
    mobile: /^1[3-9]\d{9}$/,              // mobile phone
    landline: /^0\d{2,3}-?\d{7,8}$/       // landline
  },
  employeeId: {
    pattern: /^EMP\d{6}$/,
    description: 'Employee ID: EMP + 6 digits',
    examples: {
      valid: ['EMP123456', 'EMP000001'],
      invalid: ['emp123456', 'EMP12345']
    }
  }
});

// Now only your teamโ€™s regex rules are available
rx.add('employeeId', /^EMP\d{6}$/);
rx.get('email'); // /^[a-z0-9._%+-]+@company\.com$/
rx.test('email', 'user@company.com'); // true
rx.test('email', 'user@gmail.com');   // false
rx.info('employeeId');                // view rule info
Enter fullscreen mode Exit fullscreen mode

๐Ÿงฉ Custom Scenarios

Custom Rule Injection

Need to define your companyโ€™s domain rule?

rx.inject({
  email: /^[a-z0-9._%+-]+@company.com$/,
});

rx.test('email', 'jack@company.com'); // true
rx.test('email', 'jack@gmail.com');   // false
Enter fullscreen mode Exit fullscreen mode

Batch Operations

const text = 'Emails: a@b.com, c@d.com, e@f.com';

// Highlight all
rx.highlight('email', text, '<mark>$&</mark>');
// => Emails: <mark>a@b.com</mark>, <mark>c@d.com</mark>, <mark>e@f.com</mark>

// Count matches
rx.count('email', text); // 3
Enter fullscreen mode Exit fullscreen mode

extractBatch, replaceBatch, and countBatch are also available for more complex tasks.


Group Syntax

// Multiple formats for the same type โ€” a unique feature of regex-center

// Password strength
rx.test('password:weak', '123456');          // weak
rx.test('password:medium', 'Password123');   // medium
rx.test('password:strong', 'Password123!');  // strong

// Email strictness
rx.test('email:basic', 'test@gmail.com');    // loose
rx.test('email:strict', 'user@company.com'); // RFC standard
rx.test('email:enterprise', 'admin@company.com'); // enterprise

// Numeric types
rx.test('number:integer', '123');
rx.test('number:decimal', '123.45');
rx.test('number:signed', '-123');

// View available groups
rx.info('password').groups; // ['weak', 'medium', 'strong']
Enter fullscreen mode Exit fullscreen mode

โšก Security & Performance

When injecting custom regex, regex-center automatically scans for ReDoS vulnerabilities,
preventing performance issues caused by poorly written regex patterns.

Even under heavy text-matching loads, it remains safe and stable.


๐Ÿ“š Built-in Regex Examples

Type Syntax Description Example
Email email / email:basic Basic email rx.test('email', 'user@example.com')
email:strict RFC-standard email rx.test('email:strict', 'user@company.com')
email:enterprise Enterprise email rx.test('email:enterprise', 'admin@company.com')
Phone phone:CN Chinese phone rx.test('phone:CN', '13800138000')
phone:US US phone rx.test('phone:US', '+1-555-123-4567')
ID Card idCard:CN Chinese ID rx.test('idCard:CN', '110101199003077777')
Bank Card bankCard:CN Chinese bank card rx.test('bankCard:CN', '6222600260001234567')
URL url Basic URL rx.test('url', 'https://example.com')
IP ip:v4 IPv4 rx.test('ip:v4', '192.168.1.1')
ip:v6 IPv6 rx.test('ip:v6', '2001:db8::1')
Number number:integer Integer rx.test('number:integer', '123')
number:decimal Decimal rx.test('number:decimal', '123.45')
Date date:YYYY-MM-DD Standard date rx.test('date:YYYY-MM-DD', '2024-01-01')
Password password:medium Medium rx.test('password:medium', 'Password123')
password:strong Strong rx.test('password:strong', 'Password123!')

๐Ÿ’ก Tip

  • email defaults to email:basic.
  • For more patterns (e.g. hex, UUID, domain, color), check the Regex Reference.

๐Ÿ” How It Differs from Other Libraries

Feature / Library validator.js regexr.com awesome-regex regex-center
Focus Validation functions Online testing Regex collections Regex management and automation
Usage isEmail(str) Manual testing Manual copy rx.test('email', str)
Built-in rules 80+ None External links 100+ + extensible system
Custom rules โŒ โŒ โŒ โœ… via inject
Type hints โŒ โŒ โŒ โœ… TypeScript support
Text tools โŒ โœ… Demo only โŒ โœ… extract / replace / highlight
ReDoS protection โŒ โŒ โŒ โœ… automatic
Ideal use case Validation Learning Collection Team-level regex standardization

In short:

  • validator.js is for simple business validation
  • regexr is for visualization and learning
  • awesome-regex is a regex index
  • regex-center is a โ€œregex management platformโ€ that unifies validation, testing, security, and extensibility

โœ… Conclusion

Regular expressions shouldnโ€™t become invisible technical debt.
regex-center turns regex from scattered snippets into a centralized, managed, and standardized system โ€”
so you can use regex like any other utility function.

If your team struggles with regex maintenance or wants a unified regex standard,
give regex-center a try.

Top comments (0)