Class: Regexp

Inherits:
Object
  • Object
show all
Defined in:
opal/opal/corelib/regexp.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.escape(string) ⇒ Object Also known as: quote



5
6
7
8
9
10
11
12
13
# File 'opal/opal/corelib/regexp.rb', line 5

def escape(string)
  %x{
    return string.replace(/([-[\]\/{}()*+?.^$\\| ])/g, '\\$1')
                 .replace(/[\n]/g, '\\n')
                 .replace(/[\r]/g, '\\r')
                 .replace(/[\f]/g, '\\f')
                 .replace(/[\t]/g, '\\t');
  }
end

.new(regexp, options = undefined) ⇒ Object



21
22
23
# File 'opal/opal/corelib/regexp.rb', line 21

def new(regexp, options = undefined)
  `new RegExp(regexp, options)`
end

.union(*parts) ⇒ Object



17
18
19
# File 'opal/opal/corelib/regexp.rb', line 17

def union(*parts)
  `new RegExp(parts.join(''))`
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



26
27
28
# File 'opal/opal/corelib/regexp.rb', line 26

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

#===(str) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'opal/opal/corelib/regexp.rb', line 30

def ===(str)
  %x{
    if (!str._isString && #{str.respond_to?(:to_str)}) {
      #{str = str.to_str};
    }

    if (!str._isString) {
      return false;
    }

    return self.test(str);
  }
end

#=~(string) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'opal/opal/corelib/regexp.rb', line 44

def =~(string)
  if `string === nil`
    $~ = $` = $' = nil

    return
  end

  string = Opal.coerce_to(string, String, :to_str).to_s

  %x{
    var re = self;

    if (re.global) {
      // should we clear it afterwards too?
      re.lastIndex = 0;
    }
    else {
      // rewrite regular expression to add the global flag to capture pre/post match
      re = new RegExp(re.source, 'g' + (re.multiline ? 'm' : '') + (re.ignoreCase ? 'i' : ''));
    }

    var result = re.exec(string);

    if (result) {
      #{$~ = MatchData.new(`re`, `result`)};
    }
    else {
      #{$~ = $` = $' = nil};
    }

    return result ? result.index : nil;
  }
end

#inspectObject



80
81
82
# File 'opal/opal/corelib/regexp.rb', line 80

def inspect
  `self.toString()`
end

#match(string, pos = undefined, &block) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'opal/opal/corelib/regexp.rb', line 84

def match(string, pos = undefined, &block)
  if `string === nil`
    $~ = $` = $' = nil

    return
  end

  if `string._isString == null`
    unless string.respond_to? :to_str
      raise TypeError, "no implicit conversion of #{string.class} into String"
    end

    string = string.to_str
  end

  %x{
    var re = self;

    if (re.global) {
      // should we clear it afterwards too?
      re.lastIndex = 0;
    }
    else {
      re = new RegExp(re.source, 'g' + (re.multiline ? 'm' : '') + (re.ignoreCase ? 'i' : ''));
    }

    var result = re.exec(string);

    if (result) {
      result = #{$~ = MatchData.new(`re`, `result`)};

      if (block === nil) {
        return result;
      }
      else {
        return #{block.call(`result`)};
      }
    }
    else {
      return #{$~ = $` = $' = nil};
    }
  }
end

#sourceObject Also known as: to_s



128
129
130
# File 'opal/opal/corelib/regexp.rb', line 128

def source
  `self.source`
end