regx

raw JSON →
1.0.4 verified Fri May 01 auth: no javascript maintenance

Tagged template string regular expression compiler. v1.0.4, last updated 2016 (stable, low maintenance). Parses multiline RegExp strings embedded in template literals, stripping comments and whitespace for readability. Unlike normal RegExp, allows inline comments (# //), partials (interpolated regex or string), and ignores insignificant whitespace. Flags are set at compile time. No dependencies.

error import { regx } from 'regx' results in undefined
cause Named import used but package only exports default.
fix
Use default import: import regx from 'regx'
error require('regx') returns an object with default undefined
cause CommonJS require accesses module.exports which has a default property.
fix
Use require('regx').default
error regx('i')('/pattern/') is not a function
cause Calling tag function directly instead of using as tagged template literal.
fix
Use template literal: const rx = regx('i'); const re = rx/pattern/;
error Unexpected whitespace in regex pattern
cause Whitespace at start/end of lines is automatically stripped.
fix
Use [ ] or \s to match literal whitespace characters.
gotcha Whitespace at start/end of each line is stripped. To match literal spaces, use [ ].
fix Use a character class like [ ] or \s to include whitespace.
gotcha Partials (interpolations) must be valid RegExp or strings; strings require double escaping (e.g., '\\d' for \d).
fix Pass /\d/ instead of '\d' to avoid escaping issues.
deprecated Package has not been updated since 2016; no official deprecation but maintenance is absent.
fix Consider alternatives like panda-regx or template-regexp for active maintenance.
gotcha Comments (//) only work if they are at the end of a line; inline // in the middle of a line may cause unexpected behavior.
fix Place // only at the end of a line after the regex content.
npm install regx
yarn add regx
pnpm add regx

Shows creation of a tag function with flags, multiline regex with comments, and interop with a RegExp partial.

import regx from 'regx';

const rx = regx('i');
const pattern = rx`
  ^        // start of line
  [a-z]+   // one or more letters
  $        // end of line
`;

console.log(pattern); // /^[a-z]+$/i
console.log(pattern.test('hello')); // true
console.log(pattern.test('123')); // false

// Using a partial
const digit = /\d/;
const mixed = rx`
  ${digit}+  // digits only
`;
console.log(mixed); // /\d+/i