Oniguruma to JavaScript RegExp Converter

4.3.5 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

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);
}

view raw JSON →