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

.last_match(n = nil) ⇒ Object



15
16
17
18
19
20
21
# File 'opal/opal/corelib/regexp.rb', line 15

def last_match(n=nil)
  if n.nil?
    $~
  else
    $~[n]
  end
end

.new(regexp, options = undefined) ⇒ Object



29
30
31
# File 'opal/opal/corelib/regexp.rb', line 29

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

.union(*parts) ⇒ Object



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

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

Instance Method Details

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



34
35
36
# File 'opal/opal/corelib/regexp.rb', line 34

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

#===(str) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'opal/opal/corelib/regexp.rb', line 38

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

    if (!str.$$is_string) {
      return false;
    }

    return self.test(str);
  }
end

#=~(string) ⇒ Object



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
77
78
79
80
81
82
83
84
85
# File 'opal/opal/corelib/regexp.rb', line 52

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`)};

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

#inspectObject



89
90
91
# File 'opal/opal/corelib/regexp.rb', line 89

def inspect
  `self.toString()`
end

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



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
127
128
129
130
131
132
133
134
135
# File 'opal/opal/corelib/regexp.rb', line 93

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

    return
  end

  if `string.$$is_string == 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



137
138
139
# File 'opal/opal/corelib/regexp.rb', line 137

def source
  `self.source`
end