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
- VERSION
Attributes
options[R]
source[R]
warnings[R]
Public Class Methods
new(ruby_regex, options: nil)
click to toggle source
# File lib/js_regex.rb, line 17 def initialize(ruby_regex, options: nil) @source, @options, @warnings = Conversion.of(ruby_regex, options: options) end
new!(ruby_regex, options: nil)
click to toggle source
# File lib/js_regex.rb, line 33 def self.new!(ruby_regex, options: nil) js_regex = new(ruby_regex, options: options) if js_regex.warnings.any? raise StandardError.new( "Could not fully convert the given regex #{ruby_regex.inspect}:\n" + js_regex.warnings.join("\n") ).extend(JsRegex::Error) end js_regex 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