jaynetics / js_regex Goto Github PK
View Code? Open in Web Editor NEWConverts Ruby regexes to JavaScript regexes.
License: MIT License
Converts Ruby regexes to JavaScript regexes.
License: MIT License
There are 4 failures about multiple quantifiers:
Failures:
1) JsRegex::Converter::Base#convert when there are multiple quantifiers drops adjacent/multiplicative fixes ({x}) without warning
Failure/Error:
expect(/a{4}{6}/).to\
become(/a{6}/)
expected a{6}, got (?:a{4}){6}
# ./spec/lib/js_regex/converter/base_spec.rb:165:in `block (4 levels) in <top (required)>'
2) JsRegex::Converter::Base#convert when there are multiple quantifiers drops adjacent/multiplicative ranges ({x,y}) without warning
Failure/Error:
expect(/a{2,4}{3,6}/).to\
become(/a{3,6}/)
expected a{3,6}, got (?:a{2,4}){3,6}
# ./spec/lib/js_regex/converter/base_spec.rb:170:in `block (4 levels) in <top (required)>'
3) JsRegex::Converter::Base#convert when there are multiple quantifiers drops mixed adjacent quantifiers without warning
Failure/Error:
expect(/ab{2,3}*/).to\
become(/ab*/)
expected ab*, got a(?:b{2,3})*
# ./spec/lib/js_regex/converter/base_spec.rb:175:in `block (4 levels) in <top (required)>'
4) JsRegex::Converter::Base#convert when there are multiple quantifiers drops multiple adjacent quantifiers without warning
Failure/Error:
expect(/ab{2}{3}{4}{5}/).to\
become(/ab{5}/)
expected ab{5}, got a(?:(?:(?:b{2}){3}){4}){5}
# ./spec/lib/js_regex/converter/base_spec.rb:180:in `block (4 levels) in <top (required)>'
Finished in 0.81114 seconds (files took 0.2056 seconds to load)
297 examples, 4 failures
Failed examples:
rspec ./spec/lib/js_regex/converter/base_spec.rb:164 # JsRegex::Converter::Base#convert when there are multiple quantifiers drops adjacent/multiplicative fixes ({x}) without warning
rspec ./spec/lib/js_regex/converter/base_spec.rb:169 # JsRegex::Converter::Base#convert when there are multiple quantifiers drops adjacent/multiplicative ranges ({x,y}) without warning
rspec ./spec/lib/js_regex/converter/base_spec.rb:174 # JsRegex::Converter::Base#convert when there are multiple quantifiers drops mixed adjacent quantifiers without warning
rspec ./spec/lib/js_regex/converter/base_spec.rb:179 # JsRegex::Converter::Base#convert when there are multiple quantifiers drops multiple adjacent quantifiers without warning
May refer to: e3da3f3
Thanks for an excellent gem! ๐ Works splendidly!
One thing I've noticed a few times is that errors from the underlying gem regexp_parser
can bubble up.
Would it make sense for js_regex
to wrap them in its own error classes?
Something like:
begin
# โฆ
rescue Regexp::Parser::ParserError, Regexp::Syntax::SyntaxError, โฆ => err
raise JsRegex::ParsingError, "Underlying parser library regexp_parser failed, #{err.message} (#{err.class})"
end
At some level I'd argue that errors raised by a library is also part of the public API, and right now the API of one of the dependencies is "bleeding through". Just a small thing though, not a major problem in practice.
For a simpler solution, could just update the readme and list the known base error classes that regexp_parser can raise.
Hi, I'm using your gem as a dependency of https://github.com/DavyJonesLocker/client_side_validations
After upgrading from 3.0.0 to 3.1.0, a test stopped working:
Failure:
CoreExtTest#test_regexp_remove_group_options [/Users/geremia/dev/client_side_validations/test/core_ext/cases/test_core_ext.rb:42]:
--- expected
+++ actual
@@ -1 +1 @@
-{:source=>"(something)", :options=>"g"}
+{:source=>"(?:something)", :options=>"g"}
Could you please tell me if I need to fix the test case or if there is a regression?
The commit which breaks tests is a35129f
JsRegex.new(/\$/).to_h
outputs:
{:source=>"", :options=>"g"}
hi! I think I've found a bug but I'm no expert in regexes so I'll just describe what I'm experiencing
in Ruby this regex is valid and works
Regexp.new("[a-z]{,3}").match("hey")[0]
=> "hey"
when we translate it to JS using our js_regex we get
::JsRegex.new(Regexp.new("[a-z]{,3}")).to_h.slice(:source, :options)
=> {:source=>"[a-z]{,3}", :options=>""}
but when I try it in Chrome, it matches nothing
"hey".match(new RegExp("[a-z]{,3}"))
null
however if I change the quantifier to {1,3} instead of just {,3}
"hey".match(new RegExp("[a-z]{1,3}"))
['hey', index: 0, input: 'hey', groups: undefined]
it works. but if we now use the u
flag
var regexp = new RegExp("[a-z]{,3}", "u")
Uncaught SyntaxError: Invalid regular expression: /[a-z]{,3}/: Incomplete quantifier
at new RegExp (<anonymous>)
at <anonymous>:1:14
it fails! let's replace again {,3} with {1,3}
"hey".match(new RegExp("[a-z]{1,3}", "u"))
['hey', index: 0, input: 'hey', groups: undefined]
and it works.
according to this doc a quantifier cannot be written as {,m}
so maybe js_regex should translate {,m} as {1,m}?
The readme mentions:
Possible future improvements might include an "ES6 mode" using the u flag, which would allow for more concise representations of astral plane properties and sets.
I was wondering if this improvement is still planned, or if the gem is considered complete at this point.
Hey, first of all: nice gem, I like using it!
I struggled a bit with the 'g' option. if it is set in javascript.
var re = /pattern/g;
re.test('has_pattern_in_it'); # true
re.test('has_pattern_in_it'); # false
re.test('has_pattern_in_it'); # true
re.test('has_pattern_in_it'); # false
See documentation
this is not really intuitive, so my question is: Is there a reason why the g option is set as a default or would it be okay to remove it?
> JsRegex.new(/[\/]/).to_s
=> "/[/]/"
expected: "/[\/]/"
or "/[\x2F]/"
Hi there
Would it be possible to add (optional?) lookbehind support?
I'm unclear whether it's technically hard to do, or it's due to lack of lookbehind support in JS land. As of Sept 2022, Can I Use is showing Safari as the only mainstream browser not supporting lookbehind, with support in Chrome, Edge, Firefox and Opera: https://caniuse.com/js-regexp-lookbehind. FYI I found the relevant webkit bug: https://bugs.webkit.org/show_bug.cgi?id=174931
Saw a reference in #10, but thought I'd raise as a new issue for visibility.
Many thanks for a such a fantastic library. I've been using it with no issues for a while, so was a little sad when I saw my first js_regex warning of "Dropped unsupported lookbehind assertion".
Thanks
Greg
Recently updated from 3.9.0
to 3.10.0
.
Issue noticed when trying to parse: '\-\.
(match the individual symbols apostrophe '
, hyphen -
and point .
)
When parsed by js_regex
it became '-.
which now matches all the symbols between apostrophe '
and point .
.
Previous versions parsed '\-\.
to '\x2D.
which works as intended.
MRE results:
js_regex_version: 3.9.0
regex string = '\-\.
ruby regexp = (?-mix:\A['\-\.])
js_regex = /^['\x2D.]/
warnings: []
vs.
js_regex_version: 3.10.0
regex string = '\-\.
ruby regexp = (?-mix:\A['\-\.])
js_regex = /^['-.]/
warnings: []
It's not possible for oniguruma and this module to work together in a performant manner atm.
We'd be doing both regexes each time we shell out to oniguruma instead and it'd happen for every use of \G
.
Instead we could try to add validation for this.
Basically turn everything except warn_of_unsupported_feature
into a no-op and check if the warning stack is empty after..
js_regex 2.2.0, Ruby 2.3.1.
[1] pry(main)> JsRegex.new(/\//).to_s
=> "/\\//g" # Correct
[2] pry(main)> JsRegex.new(Regexp.union(/\//)).to_s
=> "/\\//g" # Correct
[3] pry(main)> JsRegex.new(Regexp.union(/\//, /a/)).to_s
=> "/(\\\\/)|(a)/g" # Incorrect, should be "/(\\/)|(a)/g".
[4] pry(main)> Regexp.union(/\//, /a/)
=> /(?-mix:\/)|(?-mix:a)/
[5] pry(main)> JsRegex.new(/(?-mix:\/)|(?-mix:a)/).to_s
=> "/(\\/)|(a)/g" # Oddly, back to being correct.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.