Glamm: Python to JavaScript Regex Converter
Glamm is a lightweight utility that converts Python-flavored regular expressions into JavaScript-compatible ones. It helps bridge the gap between the regex syntax of the two languages, allowing you to use familiar Python patterns in a JavaScript environment.
Features
Glamm currently supports the following conversions:
- Named Capture Groups:
(?P<name>...) is converted to (?<name>...)
- Named Group References:
(?P=name) is converted to \k<name>
- Possessive Quantifiers:
*+, ++, ?+ are converted to their JavaScript equivalents.
- Start of String Anchor:
\A is converted to ^.
- End of String Anchors:
\Z and \z are converted to $.
- Inline Flags: Extracts flags like
i, s, m, u from (?is...).
- Inline Comments:
(?#...) are removed.
Code Example
Here's an example of how to use Glamm to convert a Python regex pattern:
import { glamm } from "glamm";
const pythonPattern = "(?i)(?P<username>\w+), (?P=username)@gmail.com";
const { regex } = glamm(pythonPattern);
console.log(regex);
Testing
This project uses Vitest for testing. To run the tests, use the following command:
npm test
Coverage
To generate a test coverage report, run:
npm run coverage
This will create a coverage directory with a detailed report.