glamm
v1.0.2
Published
Python regular expressions to JavaScript regex
Readme
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:
\Ais converted to^. - End of String Anchors:
\Zand\zare converted to$. - Inline Flags: Extracts flags like
i,s,m,ufrom(?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);
// Output: /(?<username>\w+), \k<username>@gmail.com/iTesting
This project uses Vitest for testing. To run the tests, use the following command:
npm testCoverage
To generate a test coverage report, run:
npm run coverageThis will create a coverage directory with a detailed report.
