Class: JsRegex

Inherits:
Object
  • Object
show all
Defined in:
lib/js_regex.rb,
lib/js_regex/node.rb,
lib/js_regex/version.rb,
lib/js_regex/converter.rb,
lib/js_regex/conversion.rb,
lib/js_regex/second_pass.rb,
lib/js_regex/converter/base.rb,
lib/js_regex/converter/context.rb,
lib/js_regex/converter/set_converter.rb,
lib/js_regex/converter/meta_converter.rb,
lib/js_regex/converter/type_converter.rb,
lib/js_regex/converter/group_converter.rb,
lib/js_regex/converter/anchor_converter.rb,
lib/js_regex/converter/escape_converter.rb,
lib/js_regex/converter/literal_converter.rb,
lib/js_regex/converter/property_converter.rb,
lib/js_regex/converter/assertion_converter.rb,
lib/js_regex/converter/freespace_converter.rb,
lib/js_regex/converter/conditional_converter.rb,
lib/js_regex/converter/backreference_converter.rb,
lib/js_regex/converter/subexpression_converter.rb,
lib/js_regex/converter/unsupported_token_converter.rb

Overview

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

Defined Under Namespace

Modules: Converter, SecondPass Classes: Conversion, Node

Constant Summary collapse

VERSION =
'3.4.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ruby_regex, options: nil) ⇒ JsRegex

Returns a new instance of JsRegex.



18
19
20
# File 'lib/js_regex.rb', line 18

def initialize(ruby_regex, options: nil)
  @source, @options, @warnings = Conversion.of(ruby_regex, options: options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/js_regex.rb', line 16

def options
  @options
end

#sourceObject (readonly)

Returns the value of attribute source.



16
17
18
# File 'lib/js_regex.rb', line 16

def source
  @source
end

#warningsObject (readonly)

Returns the value of attribute warnings.



16
17
18
# File 'lib/js_regex.rb', line 16

def warnings
  @warnings
end

Instance Method Details

#to_hObject



22
23
24
# File 'lib/js_regex.rb', line 22

def to_h
  { source: source, options: options }
end

#to_json(options = {}) ⇒ Object



26
27
28
# File 'lib/js_regex.rb', line 26

def to_json(options = {})
  to_h.to_json(options)
end

#to_sObject



30
31
32
# File 'lib/js_regex.rb', line 30

def to_s
  "/#{source.empty? ? '(?:)' : source}/#{options}"
end