Registry / serialization / oniguruma-to-es

oniguruma-to-es

JSON →
library4.3.5jsnpmunverified

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.

npm install oniguruma-to-es
INSTALL
IMPORT
SIG · ONIGURUMA-TO-ES
O
oniguruma-to-es
serializationjavascriptv4.3.5
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

toRegExp
import { toRegExp } from 'oniguruma-to-es';
const toRegExp = require('oniguruma-to-es').toRegExp;
Primary function for converting an Oniguruma pattern string to a native JavaScript RegExp object. CommonJS `require` is generally not recommended as the library is ESM-first.
toRegExpDetails
import { toRegExpDetails } from 'oniguruma-to-es';
const { toRegExpDetails } = require('oniguruma-to-es');
Provides a more detailed output object including the compiled RegExp and metadata about the conversion.
EmulatedRegExp
import { EmulatedRegExp } from 'oniguruma-to-es';
import EmulatedRegExp from 'oniguruma-to-es/EmulatedRegExp';
A custom RegExp subclass used internally for advanced feature emulation. It's needed at runtime if precompiled regexes use certain features. Must be imported as a named export from the main package.

Demonstrates converting an Oniguruma pattern with 'x' flag, duplicate named capture groups, Unicode properties, and character class intersection into a native JavaScript RegExp, then tests it.

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); }
Debug
Known issues
breakingThe `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.
fix
If direct AST access is needed, import and use functions from 'oniguruma-parser' directly. Otherwise, use `toRegExp` from `oniguruma-to-es` for pattern translation.
affects: >=4.0.0
gotchaWhile `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.
fix
Always use `oniguruma-to-es` to translate Oniguruma patterns before using them in a JavaScript environment to ensure correct behavior and semantics.
affects: >=1.0.0
gotchaSpecific 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.
fix
Ensure `oniguruma-to-es` is updated to at least v4.3.3 to benefit from bug workarounds. Thoroughly test complex regexes in all target browser environments.
affects: <4.3.3
gotchaBun 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.
fix
Update `oniguruma-to-es` to at least v4.3.1 when working with Bun, or ensure Bun itself is updated to a version greater than 1.1.34.
affects: <4.3.1
gotchaFor 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.
fix
Precompile regexes during your build process. If `EmulatedRegExp` is required for some patterns, ensure it's properly imported and bundled. Consider whether less complex patterns can avoid `EmulatedRegExp` entirely.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: toOnigurumaAst is not a function
Attempting to call the `toOnigurumaAst` function which was removed in version 4.0.0.
fix
This 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
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`.
fix
Pass 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
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.
fix
Ensure you `import { EmulatedRegExp } from 'oniguruma-to-es';` in your module where the precompiled regexes are being used.
Upgrade
Version history
4.3.5latest on npm
Audit
Dependencies
oniguruma-parserrequiredCore parsing logic for Oniguruma patterns, foundational to the translation process. The functionality was split out into this separate package starting from oniguruma-to-es v4.0.0.
Agent activity
2 hits · last 30 days
node
2
Resources
oniguruma-to-es — npm install oniguruma-to-es · libregistry