Sha256: e338d16ddff8fb3db28b07644a68f5f614c0aa26161dde087d40577337d54295

Contents?: true

Size: 1.23 KB

Versions: 13

Compression:

Stored size: 1.23 KB

Contents

class Regexp < `RegExp`
  def self.escape(string)
    `string.replace(/([.*+?^=!:${}()|[\]\\/\\])/g, '\\$1')`
  end

  def self.new(string, options = undefined)
    `new RegExp(string, options)`
  end

  def ==(other)
    `other.constructor == RegExp && #{self}.toString() === other.toString()`
  end

  alias_native :===, :test

  def =~(string)
    %x{
      var result = #{self}.exec(string);

      if (result) {
        result.$to_s    = match_to_s;
        result.$inspect = match_inspect;
        result._klass = #{MatchData};

        #{$~ = `result`};
      }
      else {
        #{$~ = nil};
      }

      return result ? result.index : nil;
    }
  end

  alias eql? ==

  alias_native :inspect, :toString

  def match(pattern)
    %x{
      var result  = #{self}.exec(pattern);

      if (result) {
        result.$to_s    = match_to_s;
        result.$inspect = match_inspect;
        result._klass = #{MatchData};

        return #{$~ = `result`};
      }
      else {
        return #{$~ = nil};
      }
    }
  end

  def to_s
    `#{self}.source`
  end

  %x{
    function match_to_s() {
      return this[0];
    }

    function match_inspect() {
      return "<#MatchData " + this[0].$inspect() + ">";
    }
  }
end

class MatchData
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
opal-0.3.37 lib/assets/javascripts/opal/regexp.rb
opal-0.3.36 lib/assets/javascripts/opal/regexp.rb
opal-0.3.35 lib/assets/javascripts/opal/regexp.rb
opal-0.3.34 lib/assets/javascripts/opal/regexp.rb
opal-0.3.33 core/regexp.rb
opal-0.3.32 core/regexp.rb
opal-0.3.31 core/regexp.rb
opal-0.3.30 core/regexp.rb
opal-0.3.29 core/regexp.rb
opal-0.3.28 core/regexp.rb
opal-0.3.27 core/regexp.rb
opal-0.3.26 core/regexp.rb
opal-0.3.25 core/regexp.rb