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
options[R]
source[R]
target[R]
warnings[R]
Public Class Methods
compatible?(ruby_regex, **kwargs)
click to toggle source
# File lib/js_regex.rb, line 38 def self.compatible?(ruby_regex, **kwargs) new!(ruby_regex, **kwargs) true rescue ConversionError false end
new(ruby_regex, **kwargs)
click to toggle source
# File lib/js_regex.rb, line 17 def initialize(ruby_regex, **kwargs) @source, @options, @warnings, @target = Conversion.of(ruby_regex, **kwargs) end
new!(ruby_regex, **kwargs)
click to toggle source
@raise JsRegex::ConversionError
# File lib/js_regex.rb, line 34 def self.new!(ruby_regex, **kwargs) new(ruby_regex, fail_fast: true, **kwargs) end
Public Instance Methods
to_h()
click to toggle source
# File lib/js_regex.rb, line 21 def to_h { source: source, options: options } end
to_json(options = {})
click to toggle source
# File lib/js_regex.rb, line 25 def to_json(options = {}) to_h.to_json(options) end
to_s()
click to toggle source
# File lib/js_regex.rb, line 29 def to_s "/#{source.empty? ? '(?:)' : source}/#{options}" end