class JsRegex
JsRegex
converts ::Regexp instances to JavaScript.
Usage:
js_regex = JsRegex.new(my_ruby_regex)
js_regex.to_h # for use in ‘new RegExp()’ js_regex.to_s # for direct injection into JavaScript
Constants
- ConversionError
- VERSION
Attributes
Public Class Methods
Source
# File lib/js_regex.rb, line 38 def self.compatible?(ruby_regex, **kwargs) new!(ruby_regex, **kwargs) true rescue ConversionError false end
Source
# File lib/js_regex.rb, line 17 def initialize(ruby_regex, **kwargs) @source, @options, @warnings, @target = Conversion.of(ruby_regex, **kwargs) end
Source
# File lib/js_regex.rb, line 34 def self.new!(ruby_regex, **kwargs) new(ruby_regex, fail_fast: true, **kwargs) end
@raise JsRegex::ConversionError
Public Instance Methods
Source
# File lib/js_regex.rb, line 25 def to_json(options = {}) to_h.to_json(options) end
Source
# File lib/js_regex.rb, line 29 def to_s "/#{source.empty? ? '(?:)' : source}/#{options}" end