Sha256: 2656fb56b7b271876e6eb3d1d597373ef224da95ef94b3ee76c061a19c7100f9

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 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, pos = undefined)
    %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

2 entries across 2 versions & 1 rubygems

Version Path
opal-0.3.41 opal/opal/regexp.rb
opal-0.3.40 lib/assets/javascripts/opal/regexp.rb