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



4
5
6
# File 'opal/opal/corelib/regexp.rb', line 4

def self.escape(string)
  `string.replace(/[\\-\\[\\]\\/\\{\\}\\(\\)\\*\\+\\?\\.\\\\\^\\$\\| ]/g, '\\\\$&')`
end

.new(regexp, options = undefined) ⇒ Object



12
13
14
# File 'opal/opal/corelib/regexp.rb', line 12

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

.union(*parts) ⇒ Object



8
9
10
# File 'opal/opal/corelib/regexp.rb', line 8

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

Instance Method Details

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



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

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

#===(str) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'opal/opal/corelib/regexp.rb', line 20

def ===(str)
  if `str._isString == null` && str.respond_to?(:to_str)
    str = str.to_str
  end

  if `str._isString == null`
    return false
  end

  `self.test(str)`
end

#=~(string) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'opal/opal/corelib/regexp.rb', line 32

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



68
69
70
# File 'opal/opal/corelib/regexp.rb', line 68

def inspect
  `self.toString()`
end

#match(string, pos = undefined) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'opal/opal/corelib/regexp.rb', line 72

def match(string, pos = undefined)
  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) {
      return #{$~ = MatchData.new(`re`, `result`)};
    }
    else {
      return #{$~ = $` = $' = nil};
    }
  }
end

#sourceObject Also known as: to_s



109
110
111
# File 'opal/opal/corelib/regexp.rb', line 109

def source
  `self.source`
end