Gathering detailed insights and metrics for oniguruma-to-es
Gathering detailed insights and metrics for oniguruma-to-es
Gathering detailed insights and metrics for oniguruma-to-es
Gathering detailed insights and metrics for oniguruma-to-es
Convert patterns from Oniguruma (the regex engine used by Ruby, TextMate grammars, etc.) to native JavaScript RegExp
npm install oniguruma-to-es
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
50 Stars
288 Commits
1 Forks
1 Watching
1 Branches
2 Contributors
Updated on 27 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
29.3%
66,919
Compared to previous day
Last week
59.5%
371,310
Compared to previous week
Last month
930,041.5%
604,592
Compared to previous month
Last year
0%
604,657
Compared to previous year
3
4
An Oniguruma to JavaScript regex transpiler that runs in the browser and on your server. Use it to:
Compared to running the Oniguruma C library via WASM bindings using vscode-oniguruma, this library is less than 4% of the size and its regexes often run much faster since they run as native JavaScript.
Oniguruma-To-ES deeply understands the hundreds of large and small differences between Oniguruma and JavaScript regex syntax and behavior, across multiple JavaScript version targets. It's obsessive about ensuring that the emulated features it supports have exactly the same behavior, even in extreme edge cases. And it's been battle-tested on thousands of real-world Oniguruma regexes used in TextMate grammars (via the Shiki library). A few uncommon features can't be perfectly emulated and allow rare differences, but if you don't want to allow this, you can set the accuracy
option to throw for such patterns (see details below).
1npm install oniguruma-to-es
1import {toRegExp} from 'oniguruma-to-es'; 2 3const str = 'β¦'; 4const pattern = 'β¦'; 5// Works with all string/regexp methods since it returns a native regexp 6str.match(toRegExp(pattern));
1<script src="https://cdn.jsdelivr.net/npm/oniguruma-to-es/dist/index.min.js"></script> 2<script> 3 const {toRegExp} = OnigurumaToES; 4</script>
toRegExp
Accepts an Oniguruma pattern and returns an equivalent JavaScript RegExp
.
[!TIP] Try it in the demo REPL.
1function toRegExp(
2 pattern: string,
3 options?: OnigurumaToEsOptions
4): RegExp | EmulatedRegExp;
OnigurumaToEsOptions
1type OnigurumaToEsOptions = { 2 accuracy?: 'default' | 'strict'; 3 avoidSubclass?: boolean; 4 flags?: string; 5 global?: boolean; 6 hasIndices?: boolean; 7 maxRecursionDepth?: number | null; 8 rules?: { 9 allowOrphanBackrefs?: boolean; 10 allowUnhandledGAnchors?: boolean; 11 asciiWordBoundaries?: boolean; 12 }; 13 target?: 'auto' | 'ES2025' | 'ES2024' | 'ES2018'; 14 verbose?: boolean; 15};
See Options for more details.
toDetails
Accepts an Oniguruma pattern and returns the details needed to construct an equivalent JavaScript RegExp
.
1function toDetails(
2 pattern: string,
3 options?: OnigurumaToEsOptions
4): {
5 pattern: string;
6 flags: string;
7 subclass?: EmulatedRegExpOptions;
8};
Note that the returned flags
might also be different than those provided, as a result of the emulation process. The returned pattern
, flags
, and subclass
properties can be provided as arguments to the EmulatedRegExp
constructor to produce the same result as toRegExp
.
If the only keys returned are pattern
and flags
, they can optionally be provided to JavaScript's RegExp
constructor instead. Setting option avoidSubclass
to true
ensures that this is always the case, by throwing an error for any patterns that rely on EmulatedRegExp
's additional handling.
toOnigurumaAst
Returns an Oniguruma AST generated from an Oniguruma pattern.
1function toOnigurumaAst(
2 pattern: string,
3 options?: {
4 flags?: string;
5 }
6): OnigurumaAst;
EmulatedRegExp
Works the same as JavaScript's native RegExp
constructor in all contexts, but can be given results from toDetails
to produce the same result as toRegExp
.
1class EmulatedRegExp extends RegExp { 2 constructor( 3 pattern: string | EmulatedRegExp, 4 flags?: string, 5 options?: EmulatedRegExpOptions 6 ); 7};
The following options are shared by functions toRegExp
and toDetails
.
accuracy
One of 'default'
(default) or 'strict'
.
Sets the level of emulation rigor/strictness.
target
.Using default accuracy
adds support for the following features, depending on target
:
ES2025
and earlier):
\X
using a close approximation of a Unicode extended grapheme cluster.\g<0>
) with a depth limit specified by option maxRecursionDepth
.ES2024
and earlier:
ES2018
:
[:graph:]
and [:print:]
using ASCII-based versions rather than the Unicode versions available for ES2024
and later. Other POSIX classes are always Unicode-based.avoidSubclass
Default: false
.
Disables advanced emulation that relies on returning a RegExp
subclass, resulting in certain patterns not being emulatable.
flags
Oniguruma flags; a string with i
, m
, x
, D
, S
, W
in any order (all optional).
Flags can also be specified via modifiers in the pattern.
[!IMPORTANT] Oniguruma and JavaScript both have an
m
flag but with different meanings. Oniguruma'sm
is equivalent to JavaScript'ss
(dotAll
).
global
Default: false
.
Include JavaScript flag g
(global
) in the result.
hasIndices
Default: false
.
Include JavaScript flag d
(hasIndices
) in the result.
maxRecursionDepth
Default: 5
.
Specifies the recursion depth limit. Supported values are integers 2
β100
and null
. If null
, any use of recursion results in an error.
Since recursion isn't infinite-depth like in Oniguruma, use of recursion also results in an error if using strict accuracy
.
Using a high limit has a small impact on performance. Generally, this is only a problem if the regex has an existing issue with runaway backtracking that recursion exacerbates. Higher limits have no effect on regexes that don't use recursion, so you should feel free to increase this if helpful.
rules
Advanced pattern options that override standard error checking and flags when enabled.
allowOrphanBackrefs
: Useful with TextMate grammars that merge backreferences across patterns.allowUnhandledGAnchors
: Applies flag y
for unsupported uses of \G
, rather than erroring.
\G
. When using this option, if a \G
is found that doesn't have a known emulation strategy, the \G
is simply removed and JavaScript's y
(sticky
) flag is added. This might lead to some false positives and negatives, but is useful for non-critical matching (like syntax highlighting) when having some mismatches is better than not working.asciiWordBoundaries
: Use ASCII-based \b
and \B
, which increases performance.target
One of 'auto'
(default), 'ES2025'
, 'ES2024'
, or 'ES2018'
.
JavaScript version used for generated regexes. Using auto
detects the best value based on your environment. Later targets allow faster processing, simpler generated source, and support for additional features.
ES2018
: Uses JS flag u
.
ES2024
: Uses JS flag v
.
ES2025
: Uses JS flag v
and allows use of flag groups and duplicate group names.
verbose
Default: false
.
Disables optimizations that simplify the pattern when it doesn't change the meaning.
Following are the supported features by target. The official Oniguruma syntax doc doesn't cover many of the finer details described here.
[!NOTE] Targets
ES2024
andES2025
have the same emulation capabilities. Resulting regexes might have different source and flags, but they match the same strings. Seetarget
.
Notice that nearly every feature below has at least subtle differences from JavaScript. Some features listed as unsupported are not emulatable using native JavaScript regexes, but support for others might be added in future versions of this library. Unsupported features throw an error.
Feature | Example | ES2018 | ES2024+ | Subfeatures & JS differences | |
---|---|---|---|---|---|
Flags | Supported in top-level flags and pattern modifiers | ||||
Ignore case | i | β | β |
β Unicode case folding (same as JS with flag u , v ) | |
Dot all | m | β | β |
β Equivalent to JS flag s | |
Extended | x | β | β |
β Unicode whitespace ignored β Line comments with # β Whitespace/comments allowed between a token and its quantifier β Whitespace/comments between a quantifier and the ? /+ that makes it lazy/possessive changes it to a quantifier chainβ Whitespace/comments separate tokens (ex: \1 0 )β Whitespace and # not ignored in char classes | |
Currently supported only in top-level flags | |||||
Digit is ASCII | D | β | β |
β ASCII \d , \p{Digit} , [[:digit:]] | |
Space is ASCII | S | β | β |
β ASCII \s , \p{Space} , [[:space:]] | |
Word is ASCII | W | β | β |
β ASCII \b , \w , \p{Word} , [[:word:]] | |
Pattern modifiers | Group | (?im-x:β¦) | β | β |
β Unicode case folding for i β Allows enabling and disabling the same flag (priority: disable) β Allows lone or multiple - |
Directive | (?im-x) | β | β |
β Continues until end of pattern or group (spanning alternatives) | |
Characters | Literal | E , ! | β | β |
β Code point based matching (same as JS with flag u , v )β Standalone ] , { , } don't require escaping |
Identity escape | \E , \! | β | β |
β Different allowed set than JS β Allows multibyte chars | |
Escaped metachar | \\ , \. | β | β |
β Same as JS | |
Control code escape | \t | β | β |
β The JS set plus \a , \e | |
\xNN | \x7F | β | β |
β Allows 1 hex digit β Above 7F , is UTF-8 encoded byte (β JS)β Error for invalid encoded bytes | |
\uNNNN | \uFFFF | β | β |
β Same as JS with flag u , v | |
\x{β¦} | \x{A} | β | β |
β Allows leading 0s up to 8 total hex digits | |
Escaped num | \20 | β | β |
β Can be backref, error, null, octal, identity escape, or any of these combined with literal digits, based on complex rules that differ from JS β Always handles escaped single digit 1-9 outside char class as backref β Allows null with 1-3 0s β Error for octal > 177 | |
Control | \cA , \C-A | β | β |
β With A-Za-z (JS: only \c form) | |
Character sets | Digit | \d , \D | β | β |
β Unicode by default (β JS) |
Hex digit | \h , \H | β | β |
β ASCII | |
Whitespace | \s , \S | β | β |
β Unicode by default β No JS adjustments to Unicode set (β \uFEFF , +\x85 ) | |
Word | \w , \W | β | β |
β Unicode by default (β JS) | |
Dot | . | β | β |
β Excludes only \n (β JS) | |
Any | \O | β | β |
β Any char (with any flags) β Identity escape in char class | |
Not newline | \N | β | β |
β Identity escape in char class | |
Unicode property |
\p{L} ,\P{L}
| β [1] | β |
β Binary properties β Categories β Scripts β Aliases β POSIX properties β Invert with \p{^β¦} , \P{^β¦} β Insignificant spaces, underscores, and casing in names β \p , \P without { is an identity escapeβ Error for key prefixes β Error for props of strings β Blocks (wontfix[2]) | |
Variable-length sets | Newline | \R | β | β |
β Matched atomically |
Grapheme | \X | βοΈ | βοΈ |
β Uses a close approximation β Matched atomically | |
Character classes | Base | [β¦] , [^β¦] | β | β |
β Unescaped - outside of range is literal in some contexts (different than JS rules in any mode)β Error for unescaped [ that doesn't form nested classβ Allows leading unescaped ] β Fewer chars require escaping than JS |
Empty | [] , [^] | β | β |
β Error | |
Range | [a-z] | β | β |
β Same as JS with flag u , v | |
POSIX class |
[[:word:]] ,[[:^word:]]
| βοΈ[3] | β |
β All use Unicode definitions | |
Nested class | [β¦[β¦]] | βοΈ[4] | β |
β Same as JS with flag v | |
Intersection | [β¦&&β¦] | β | β |
β Doesn't require nested classes for intersection of union and ranges | |
Assertions | Line start, end | ^ , $ | β | β |
β Always "multiline" β Only \n as newline |
String start, end | \A , \z | β | β |
β Same as JS ^ $ without JS flag m | |
String end or before terminating newline | \Z | β | β |
β Only \n as newline | |
Search start | \G | βοΈ | βοΈ |
β Common uses supported | |
Lookaround |
(?=β¦) ,(?!β¦) ,(?<=β¦) ,(?<!β¦)
| β | β |
β Same as JS β Allows variable-length quantifiers and alternation within lookbehind | |
Word boundary | \b , \B | β | β |
β Unicode based (β JS) | |
Quantifiers | Greedy, lazy | * , +? , {2,} , etc. | β | β |
β Includes all JS forms β Adds {,n} for min 0β Explicit bounds have upper limit of 100,000 (unlimited in JS) β Error with assertions (same as JS with flag u , v ) |
Possessive | ?+ , *+ , ++ | β | β |
β + suffix doesn't make {β¦} interval quantifiers possessive (creates a quantifier chain) | |
Chained | ** , ??+* , {2,3}+ , etc. | β | β |
β Further repeats the preceding repetition | |
Groups | Noncapturing | (?:β¦) | β | β |
β Same as JS |
Atomic | (?>β¦) | β | β |
β Supported | |
Capturing | (β¦) | β | β |
β Is noncapturing if named capture present | |
Named capturing |
(?<a>β¦) ,(?'a'β¦)
| β | β |
β Duplicate names allowed (including within the same alternation path) unless directly referenced by a subroutine β Error for names invalid in Oniguruma or JS | |
Backreferences | Numbered | \1 | β | β |
β Error if named capture used β Refs the most recent of a capture/subroutine set |
Enclosed numbered, relative |
\k<1> ,\k'1' ,\k<-1> ,\k'-1'
| β | β |
β Error if named capture used β Allows leading 0s β Refs the most recent of a capture/subroutine set β \k without < ' is an identity escape | |
Named |
\k<a> ,\k'a'
| β | β |
β For duplicate group names, rematch any of their matches (multiplex) β Refs the most recent of a capture/subroutine set (no multiplex) β Combination of multiplex and most recent of capture/subroutine set if duplicate name is indirectly created by a subroutine | |
To nonparticipating groups | βοΈ | βοΈ |
β Error if group to the right[5] β Duplicate names (and subroutines) to the right not included in multiplex β Fail to match (or don't include in multiplex) ancestor groups and groups in preceding alternation paths β Some rare cases are indeterminable at compile time and use the JS behavior of matching an empty string | ||
Subroutines | Numbered, relative |
\g<1> ,\g'1' ,\g<-1> ,\g'-1' ,\g<+1> ,\g'+1'
| β | β |
β Allowed before reffed group β Can be nested (any depth) β Doesn't alter backref nums β Reuses flags from the reffed group (ignores local flags) β Replaces most recent captured values (for backrefs) β \g without < ' is an identity escapeβ Error if named capture used |
Named |
\g<a> ,\g'a'
| β | β |
β Same behavior as numbered β Error if reffed group uses duplicate name | |
Recursion | Full pattern |
\g<0> ,\g'0'
| βοΈ | βοΈ |
β Has depth limit[6] |
Numbered, relative, named |
(β¦\g<1>?β¦) ,(β¦\g<-1>?β¦) ,(?<a>β¦\g<a>?β¦) , etc.
| βοΈ | βοΈ |
β Has depth limit[6] | |
Other | Comment group | (?#β¦) | β | β |
β Allows escaping \) , \\ β Comments allowed between a token and its quantifier β Comments between a quantifier and the ? /+ that makes it lazy/possessive changes it to a quantifier chain |
Alternation | β¦|β¦ | β | β |
β Same as JS | |
Keep | \K | βοΈ | βοΈ |
β Supported if at top level and no top-level alternation is used | |
JS features unknown to Oniguruma are handled using Oniguruma syntax | β | β |
β \u{β¦} is an errorβ [\q{β¦}] matches q , etc.β [a--b] includes the invalid reversed range a to - | ||
Invalid Oniguruma syntax | β | β |
β Error |
The table above doesn't include all aspects that Oniguruma-To-ES emulates (including error handling, most aspects that work the same as in JavaScript, and many aspects of non-JavaScript features that work the same in the other regex flavors that support them).
ES2018
doesn't allow using Unicode property names added in JavaScript specifications after ES2018.Inβ¦
prefix) are easily emulatable but their character data would significantly increase library weight. They're also a flawed and arguably unuseful feature, given the ability to use Unicode scripts and other properties.ES2018
, the specific POSIX classes [:graph:]
and [:print:]
use ASCII-based versions rather than the Unicode versions available for target ES2024
and later, and they result in an error if using strict accuracy
.ES2018
doesn't support nested negated character classes.\10
or higher and not as many capturing groups are defined to the left (it's an octal or identity escape).maxRecursionDepth
. Use of backreferences when the recursed subpattern contains captures isn't yet supported. Patterns that would error in Oniguruma due to triggering infinite recursion might find a match in Oniguruma-To-ES since recursion is bounded (future versions will detect this and error at transpilation time).The following don't yet have any support, and throw errors. They're all infrequently-used features, with most being extremely rare.
\y
, \Y
.P
(POSIX is ASCII) and y{g}
/y{w}
(grapheme boundary modes).(?C)
, ignore-case is ASCII (?I)
, find longest (?L)
.(?~β¦)
, etc.(?(β¦)β¦)
, etc.\cx
, \C-x
; meta \M-x
, \M-\C-x
; bracketed octals \o{β¦}
; octal UTF-8 encoded bytes (β₯ \200
).\x{H H β¦}
, \o{O O β¦}
.(?{β¦})
, etc.Oniguruma-To-ES fully supports mixed case-sensitivity (and handles the Unicode edge cases) regardless of JavaScript target. It also restricts Unicode properties to those supported by Oniguruma and the target JavaScript version.
Oniguruma-To-ES focuses on being lightweight to make it better for use in browsers. This is partly achieved by not including heavyweight Unicode character data, which imposes a couple of minor/rare restrictions:
ES2018
. Use target ES2024
or later if you need support for these features.ES2025
, a handful of Unicode properties that target a specific character case (ex: \p{Lower}
) can't be used case-insensitively in patterns that contain other characters with a specific case that are used case-sensitively.
A\p{Lower}
, (?i:A\p{Lower})
, (?i:A)\p{Lower}
, (?i:A(?-i:\p{Lower}))
, and \w(?i:\p{Lower})
, but not A(?i:\p{Lower})
.JsRegex transpiles Onigmo regexes to JavaScript (Onigmo is a fork of Oniguruma with mostly shared syntax and behavior). It's written in Ruby and relies on the Regexp::Parser Ruby gem, which means regexes must be pre-transpiled on the server to use them in JavaScript. Note that JsRegex doesn't always translate edge case behavior differences.
Oniguruma-To-ES was created by Steven Levithan.
If you want to support this project, I'd love your help by contributing improvements, sharing it with others, or sponsoring ongoing development.
Β© 2024βpresent. MIT License.
No vulnerabilities found.
No security vulnerabilities found.