Project

js_regex

0.63
A long-lived project that still receives updates
JsRegex converts Ruby's native regular expressions for JavaScript, taking care of various incompatibilities and returning warnings for unsolvable differences.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

JsRegex

Gem Version Build Status Build Status Coverage

This is a Ruby gem that translates Ruby's regular expressions to various JavaScript flavors.

It can handle almost all of Ruby's regex features, unlike a search-and-replace approach. If any incompatibilities remain, it returns helpful warnings to indicate them.

Installation

Add it to your gemfile or run

gem install js_regex

Usage

Basic usage

In Ruby:

require 'js_regex'

ruby_hex_regex = /0x\h+/i

js_regex = JsRegex.new(ruby_hex_regex)

js_regex.warnings # => []
js_regex.source # => '0x[0-9A-Fa-f]+'
js_regex.options # => 'i'

To inject the result directly into JavaScript, use #to_s or String interpolation. E.g. in inline JavaScript in HAML or SLIM you can simply do:

var regExp = #{js_regex};

Use #to_json if you want to send it as JSON or #to_h to include it as a data attribute of a DOM element.

render json: js_regex

js_regex.to_h # => { source: '[0-9A-Fa-f]+', options: '' }

To turn the data attribute or parsed JSON back into a RegExp in JavaScript, use the new RegExp() constructor:

var regExp = new RegExp(jsonObj.source, jsonObj.options);

Heed the Warnings

You might have noticed the empty warnings array in the example above:

js_regex = JsRegex.new(ruby_hex_regex)
js_regex.warnings # => []

If this array isn't empty, that means that your Ruby regex contained some stuff that can't be carried over to JavaScript. You can still use the result, but this is not recommended. Most likely it won't match the same strings as your Ruby regex.

advanced_ruby_regex = /(?<!fizz)buzz/

js_regex = JsRegex.new(advanced_ruby_regex)
js_regex.warnings # => ["Dropped unsupported negative lookbehind '(?<!fizz)' at index 0 (requires at least `target: 'ES2018'`)"]
js_regex.source # => 'buzz'

There is also a strict initializer, JsRegex::new!, which raises a JsRegex::Error if there are incompatibilites. This is particularly useful if you use JsRegex to convert regex-like strings, e.g. strings entered by users, as a JsRegex::Error might also occur if the given regex is invalid:

begin
  user_input = '('
  JsRegex.new(user_input)
rescue JsRegex::Error => e
  e.message # => "Premature end of pattern (missing group closing parenthesis)"
end

Modifying RegExp options/flags

An options: argument lets you append options (a.k.a. "flags") to the output:

JsRegex.new(/x/i, options: 'g').to_h
# => { source: 'x', options: 'gi' }

Set the g flag like this if you want to use the regex to find or replace multiple matches per string.

Converting for modern JavaScript

A target: argument can be given to target more recent versions of JS and unlock extra features or nicer output. 'ES2009' is the default target. 'ES2015' and 'ES2018' are also available. Please note that in 2022, Safari still doesn't fully support ES2018, so you should only use 'ES2018' if you either don't need to support Safari or don't plan to use lookbehinds or word boundary anchors (see supported features for details).

# ES2015 uses the u-flag to avoid lengthy escape sequences
JsRegex.new(/😋/, target: 'ES2009').to_s # => "/(?:\\uD83D\\uDE0B)/"
JsRegex.new(/😋/, target: 'ES2015').to_s # => "/😋/u"

# ES2018 adds support for lookbehinds, properties etc.
JsRegex.new(/foo\K\p{ascii}/, target: 'ES2015').to_s # => "/foo[\x00-\x7f]/"
JsRegex.new(/foo\K\p{ascii}/, target: 'ES2018').to_s # => "/(?<=foo)\p{ASCII}/"

Supported Features

These are the supported features by target.

Unsupported features are at the bottom of this list.

When converting a Regexp that contains unsupported features, corresponding parts of the pattern are dropped from the result and warnings are emitted.

Description Example ES2009 ES2015 ES2018
escaped meta chars \\A
dot matching astral chars /./ =~ '😋'
Ruby's multiline mode [1] /.+/m
Ruby's free-spacing mode / http (s?) /x
possessive quantifiers [2] ++, *+, ?+
atomic groups [2] a(?>bc|b)c
conditionals [2] (?('a')b|c)
option groups/switches (?i-m:..), (?x)..
local encoding options (?u:\w)
absence groups /\*(?~\*/)\*/
chained quantifiers /A{2}{4}/ =~ 'A' * 8
hex types \h and \H \H\h{6}
bell and escape shortcuts \a, \e
all literals, including \n eval("/\n/")
newline-ready anchor \Z last word\Z
generic linebreak \R data.split(/\R/)
meta and control escapes /\M-\C-X/
numeric backreferences \1, \k<1>
relative backreferences \k<-1>
named backreferences \k<foo>
numeric subexp calls \g<1>
relative subexp calls \g<-1>
named subexp calls \g<foo>
recursive subexp calls [3] \g<0>
nested sets [a-z[A-Z]]
types in sets [a-z\h]
properties in sets [a-z\p{sc}]
set intersections [\w&&[^a]]
recursive set negation [^a[^b]]
posix types [[:alpha:]]
posix negations [[:^alpha:]]
codepoint lists \u{61 63 1F601}
unicode properties \p{Dash}, \p{Thai}
unicode abbreviations \p{Mong}, \p{Sc}
unicode negations \p{^L}, \P{L}
astral plane properties [2] \p{emoji}
astral plane literals [2] 😁
astral plane ranges [2] [😁-😲]
capturing group names [4] (?<a>, (?'a' X X
extended grapheme type \X X X
lookbehinds (?<=a), (?<!a) X X ✓ [5]
keep marks \K X X ✓ [5]
sane word boundaries [6] \b, \B X X ✓ [5]
nested keep mark /a(b\Kc)d/ X X X
backref by recursion level \k<1+1> X X X
previous match anchor \G X X X
variable length absence (?~(a+|bar)) X X X
comment groups [4] (?#comment) X X X
inline comments [4] /[a-z] # comment/x X X X

[1] Keep in mind that Ruby's multiline mode is more of a "dot-all mode" and totally different from JavaScript's multiline mode.

[2] See here for information about how this is achieved.

[3] Limited to 5 levels of depth.

[4] These are dropped without warning because they can be removed without affecting the matching behavior.

[5] Not compatible with Safari. Regexps with this feature, transpiled for this target, will lead to JS errors in Safari because it still doesn't support lookbehinds.

[6] When targetting ES2018, \b and \B are replaced with a lookbehind/lookahead solution. For other targets, they are carried over as is, but generate a warning because they only recognize ASCII word chars in JavaScript (irrespective of the u-flag).

How it Works

JsRegex uses the gem regexp_parser to parse a Ruby Regexp.

It traverses the AST returned by regexp_parser depth-first, and converts it to its own tree of equivalent JavaScript RegExp tokens, marking some nodes for treatment in a second pass.

The second pass then carries out all modifications that require knowledge of the complete tree.

After the second pass, JsRegex flat-maps the final tree into a new source string.

Many Regexp tokens work in JavaScript just as they do in Ruby, or allow for a straightforward replacement, but some conversions are a little more involved.

Atomic groups and possessive quantifiers are missing in JavaScript, so the only way to emulate their behavior is by substituting them with backreferenced lookahead groups.

Astral plane characters convert to ranges of surrogate pairs when targetting ES2009 (which doesn't support astral plane chars).

Properties and posix classes expand to equivalent character sets, or surrogate pair alternations if necessary. The gem regexp_property_values helps by reading out their codepoints from Onigmo.

Character sets a.k.a. bracket expressions offer many more features in Ruby compared to JavaScript. To work around this, JsRegex calls on the gem character_set to calculate the matched codepoints of the whole set and build a completely new set string for all except the most simple cases.

Conditionals expand to equivalent alternations in the second pass, e.g. (<)?foo(?(1)>) expands to (?:<foo>|foo) (simplified example).

Subexpression calls are replaced with the conversion result of their target, e.g. (.{3})\g<1> expands to (.{3})(.{3}).

The tricky bit here is that these expressions may be nested, and that their expansions may increase the capturing group count. This means that any following backreferences need an update. E.g. (.{3})\g<1>(.)\2 (which matches strings like "FooBarXX") converts to (.{3})(.{3})(.)\3.

Contributions

Feel free to send suggestions, point out issues, or submit pull requests.

Outlook

The gem is pretty feature-complete at this point. The remaining unsupported features listed above are either impossible or impractical to replicate in JavaScript. The generated output could still be made more concise in some cases, maybe through usage of the s-flag. Finally, ES2018 might become the default target when IE is sufficiently dead and Safari catches up with lookbehinds.