Oniguruma to JavaScript RegExp Converter
Oniguruma-To-ES is a JavaScript library designed to accurately translate Oniguruma regular expression patterns into equivalent native JavaScript RegExp objects. Currently at v4.3.5, the library maintains an active release cadence, frequently delivering bug fixes and feature enhancements. A key differentiator is its ability to support approximately 99.99% of Oniguruma regex features, making it a robust alternative to WASM-based Oniguruma implementations like `vscode-oniguruma`, offering a significantly smaller bundle size and often faster execution by leveraging native JavaScript regex engines. It deeply understands and compensates for the numerous syntactic and behavioral disparities between Oniguruma and JavaScript, including differences in flag support, group naming rules, and Unicode handling. The library is built upon `oniguruma-parser` and `Regex+`, ensuring battle-tested reliability from extensive use in TextMate grammars. Developers can also precompile regexes to further optimize bundle size and runtime performance, though the `EmulatedRegExp` class may still be required for advanced feature emulation.
Common errors
-
TypeError: toOnigurumaAst is not a function
cause Attempting to call the `toOnigurumaAst` function which was removed in version 4.0.0.fixThis functionality has moved to the `oniguruma-parser` library. If you need AST access, use `oniguruma-parser`. Otherwise, use `toRegExp` from `oniguruma-to-es` to get a JavaScript RegExp object. -
SyntaxError: Invalid regular expression: /.../: Invalid group name
cause This error often occurs when an Oniguruma pattern, particularly one using duplicate named capture groups or other non-standard JavaScript regex features, is directly used to create a `RegExp` in JavaScript without prior translation by `oniguruma-to-es`.fixPass your Oniguruma pattern string through `oniguruma-to-es.toRegExp(pattern)` to translate it into a valid JavaScript RegExp object before use. -
ReferenceError: EmulatedRegExp is not defined
cause This typically happens when using precompiled regexes that rely on the `EmulatedRegExp` class, but the class itself has not been imported or is not available in the current scope.fixEnsure you `import { EmulatedRegExp } from 'oniguruma-to-es';` in your module where the precompiled regexes are being used.
Warnings
- breaking The `toOnigurumaAst` function was removed in v4.0.0. Its parsing functionality has been migrated to the separate `oniguruma-parser` library. Direct users of `toOnigurumaAst` must update their code to use `oniguruma-parser` instead.
- gotcha While `oniguruma-to-es` aims for full emulation, Oniguruma's default behaviors (e.g., `\d` as Unicode, backreferences to duplicate group names, implicit non-capturing groups with named groups) are transparently translated. Direct comparison of raw Oniguruma patterns with native JavaScript RegExp behavior will show differences. The library handles these internally, producing functionally equivalent JavaScript regexes.
- gotcha Specific Safari (WebKit) versions exhibited bugs related to nested negated character classes and escaped hyphens within character classes. While versions >=4.3.2 and >=4.3.3 include workarounds, complex regexes might still behave unexpectedly on very old or non-standard WebKit-based browsers.
- gotcha Bun versions <= 1.1.34 contained a parser bug that could affect `oniguruma-to-es`. Version 4.3.1 of this library includes a workaround. Users running on affected Bun runtimes might experience issues without the update.
- gotcha For optimal bundle size and runtime performance, precompiling Oniguruma regexes at build time is recommended. However, regexes utilizing certain advanced features will still require the runtime dependency on the `EmulatedRegExp` class (approx. 3 kB minzipped) to maintain full Oniguruma behavior.
Install
-
npm install oniguruma-to-es -
yarn add oniguruma-to-es -
pnpm add oniguruma-to-es
Imports
- toRegExp
const toRegExp = require('oniguruma-to-es').toRegExp;import { toRegExp } from 'oniguruma-to-es'; - toRegExpDetails
const { toRegExpDetails } = require('oniguruma-to-es');import { toRegExpDetails } from 'oniguruma-to-es'; - EmulatedRegExp
import EmulatedRegExp from 'oniguruma-to-es/EmulatedRegExp';
import { EmulatedRegExp } from 'oniguruma-to-es';
Quickstart
import { toRegExp } from 'oniguruma-to-es';
const onigurumaPattern = String.raw`(?x)
(?<n>\d) (?<n>\p{greek}) \k<n>
([0a-z&&\h]){,2}
`;
try {
const jsRegExp = toRegExp(onigurumaPattern, { target: 'ES2018' });
console.log('Converted RegExp:', jsRegExp.source);
console.log('Flags:', jsRegExp.flags);
// Example usage (note: the converted regex is /(?<n>\p{Nd})(\p{sc=Greek})(?>\2|\1)(?:[[0a-z]&&\p{AHex}]){0,2}/v)
const testString = '1α1';
const match = testString.match(jsRegExp);
if (match) {
console.log('Match found:', match[0]);
console.log('Named capture group 'n':', match.groups?.n);
} else {
console.log('No match found.');
}
} catch (error) {
console.error('Error converting regex:', error);
}