Class: Regexp
- Defined in:
- opal/opal/corelib/regexp.rb,
opal/opal/corelib/marshal/write_buffer.rb
Class Method Summary collapse
- .allocate ⇒ Object
- .escape(string) ⇒ Object (also: quote)
- .last_match(n = nil) ⇒ Object
- .new(regexp, options = undefined) ⇒ Object (also: compile)
- .union(*parts) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #===(string) ⇒ Object
- #=~(string) ⇒ Object
- #__marshal__(buffer) ⇒ Object
- #casefold? ⇒ Boolean
- #freeze ⇒ Object
- #inspect ⇒ Object
- #match(string, pos = undefined, &block) ⇒ Object
- #match?(string, pos = undefined) ⇒ Boolean
- #named_captures ⇒ Object
- #names ⇒ Object
- #options ⇒ Object
- #source ⇒ Object (also: #to_s)
- #~ ⇒ Object
Class Method Details
.allocate ⇒ Object
13 14 15 16 17 |
# File 'opal/opal/corelib/regexp.rb', line 13 def allocate allocated = super `#{allocated}.uninitialized = true` allocated end |
.escape(string) ⇒ Object Also known as: quote
19 20 21 22 23 24 |
# File 'opal/opal/corelib/regexp.rb', line 19 def escape(string) %x{ string = $coerce_to(string, #{::String}, 'to_str'); return Opal.escape_regexp(string); } end |
.last_match(n = nil) ⇒ Object
26 27 28 29 30 31 32 |
# File 'opal/opal/corelib/regexp.rb', line 26 def last_match(n = nil) if n.nil? $~ elsif $~ $~[n] end end |
.new(regexp, options = undefined) ⇒ Object Also known as: compile
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 77 def new(regexp, = undefined) %x{ if (regexp.$$is_regexp) { return new RegExp(regexp); } regexp = #{::Opal.coerce_to!(regexp, ::String, :to_str)}; if (regexp.charAt(regexp.length - 1) === '\\' && regexp.charAt(regexp.length - 2) !== '\\') { #{::Kernel.raise ::RegexpError, "too short escape sequence: /#{regexp}/"} } regexp = regexp.replace('\\A', '^').replace('\\z', '$') if (options === undefined || #{!}) { return new RegExp(regexp); } if (options.$$is_number) { var temp = ''; if (#{IGNORECASE} & options) { temp += 'i'; } if (#{MULTILINE} & options) { temp += 'm'; } options = temp; } else { options = 'i'; } return new RegExp(regexp, options); } end |
.union(*parts) ⇒ Object
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 65 66 67 68 69 70 71 72 73 74 75 |
# File 'opal/opal/corelib/regexp.rb', line 34 def union(*parts) %x{ var is_first_part_array, quoted_validated, part, options, each_part_options; if (parts.length == 0) { return /(?!)/; } // return fast if there's only one element if (parts.length == 1 && parts[0].$$is_regexp) { return parts[0]; } // cover the 2 arrays passed as arguments case is_first_part_array = parts[0].$$is_array; if (parts.length > 1 && is_first_part_array) { #{::Kernel.raise ::TypeError, 'no implicit conversion of Array into String'} } // deal with splat issues (related to https://github.com/opal/opal/issues/858) if (is_first_part_array) { parts = parts[0]; } options = undefined; quoted_validated = []; for (var i=0; i < parts.length; i++) { part = parts[i]; if (part.$$is_string) { quoted_validated.push(#{escape(`part`)}); } else if (part.$$is_regexp) { each_part_options = #{`part`.}; if (options != undefined && options != each_part_options) { #{::Kernel.raise ::TypeError, 'All expressions must use the same options'} } options = each_part_options; quoted_validated.push('('+part.source+')'); } else { quoted_validated.push(#{escape(`part`.to_str)}); } } } # Take advantage of logic that can parse options from JS Regex new(`quoted_validated`.join('|'), `options`) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
113 114 115 |
# File 'opal/opal/corelib/regexp.rb', line 113 def ==(other) `other instanceof RegExp && self.toString() === other.toString()` end |
#===(string) ⇒ Object
117 118 119 |
# File 'opal/opal/corelib/regexp.rb', line 117 def ===(string) `#{match(::Opal.coerce_to?(string, ::String, :to_str))} !== nil` end |
#=~(string) ⇒ Object
121 122 123 |
# File 'opal/opal/corelib/regexp.rb', line 121 def =~(string) match(string) && $~.begin(0) end |
#__marshal__(buffer) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 83 def __marshal__(buffer) buffer.save_link(self) buffer.write_ivars_prefix(self) buffer.write_extends(self) buffer.write_user_class(::Regexp, self) buffer.append('/') buffer.write_regexp(self) buffer.write_ivars_suffix(self) end |
#casefold? ⇒ Boolean
303 304 305 |
# File 'opal/opal/corelib/regexp.rb', line 303 def casefold? `self.ignoreCase` end |
#freeze ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'opal/opal/corelib/regexp.rb', line 125 def freeze # Specialized version of freeze, because the $$gm and $$g properties need to be set # especially for RegExp. return self if frozen? %x{ if (!self.hasOwnProperty('$$g')) { $prop(self, '$$g', null); } if (!self.hasOwnProperty('$$gm')) { $prop(self, '$$gm', null); } return $freeze(self); } end |
#inspect ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'opal/opal/corelib/regexp.rb', line 139 def inspect # Use a regexp to extract the regular expression and the optional mode modifiers from the string. # In the regular expression, escape any front slash (not already escaped) with a backslash. %x{ var regexp_format = /^\/(.*)\/([^\/]*)$/; var value = self.toString(); var matches = regexp_format.exec(value); if (matches) { var regexp_pattern = matches[1]; var regexp_flags = matches[2]; var chars = regexp_pattern.split(''); var chars_length = chars.length; var char_escaped = false; var regexp_pattern_escaped = ''; for (var i = 0; i < chars_length; i++) { var current_char = chars[i]; if (!char_escaped && current_char == '/') { regexp_pattern_escaped = regexp_pattern_escaped.concat('\\'); } regexp_pattern_escaped = regexp_pattern_escaped.concat(current_char); if (current_char == '\\') { if (char_escaped) { // does not over escape char_escaped = false; } else { char_escaped = true; } } else { char_escaped = false; } } return '/' + regexp_pattern_escaped + '/' + regexp_flags; } else { return value; } } end |
#match(string, pos = undefined, &block) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'opal/opal/corelib/regexp.rb', line 177 def match(string, pos = undefined, &block) %x{ if (self.uninitialized) { #{::Kernel.raise ::TypeError, 'uninitialized Regexp'} } if (pos === undefined) { if (string === nil) return #{$~ = nil}; var m = self.exec($coerce_to(string, #{::String}, 'to_str')); if (m) { #{$~ = ::MatchData.new(`self`, `m`)}; return block === nil ? #{$~} : #{yield $~}; } else { return #{$~ = nil}; } } pos = $coerce_to(pos, #{::Integer}, 'to_int'); if (string === nil) { return #{$~ = nil}; } string = $coerce_to(string, #{::String}, 'to_str'); if (pos < 0) { pos += string.length; if (pos < 0) { return #{$~ = nil}; } } // global RegExp maintains state, so not using self/this var md, re = Opal.global_regexp(self); while (true) { md = re.exec(string); if (md === null) { return #{$~ = nil}; } if (md.index >= pos) { #{$~ = ::MatchData.new(`re`, `md`)}; return block === nil ? #{$~} : #{yield $~}; } re.lastIndex = md.index + 1; } } end |
#match?(string, pos = undefined) ⇒ Boolean
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'opal/opal/corelib/regexp.rb', line 226 def match?(string, pos = undefined) %x{ if (self.uninitialized) { #{::Kernel.raise ::TypeError, 'uninitialized Regexp'} } if (pos === undefined) { return string === nil ? false : self.test($coerce_to(string, #{::String}, 'to_str')); } pos = $coerce_to(pos, #{::Integer}, 'to_int'); if (string === nil) { return false; } string = $coerce_to(string, #{::String}, 'to_str'); if (pos < 0) { pos += string.length; if (pos < 0) { return false; } } // global RegExp maintains state, so not using self/this var md, re = Opal.global_regexp(self); md = re.exec(string); if (md === null || md.index < pos) { return false; } else { return true; } } end |
#named_captures ⇒ Object
267 268 269 270 271 272 273 274 275 |
# File 'opal/opal/corelib/regexp.rb', line 267 def named_captures source.scan(/\(?<(\w+)>/, no_matchdata: true) # Scan for capture groups .map(&:first) # Get the first regexp match (\w+) .each_with_index # Add index to an iterator .group_by(&:first) # Group by the capture group names .transform_values do |i| # Convert hash values i.map { |j| j.last + 1 } # Drop the capture group names; increase indexes by 1 end end |
#names ⇒ Object
263 264 265 |
# File 'opal/opal/corelib/regexp.rb', line 263 def names source.scan(/\(?<(\w+)>/, no_matchdata: true).map(&:first).uniq end |
#options ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'opal/opal/corelib/regexp.rb', line 285 def # Flags would be nice to use with this, but still experimental - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/flags %x{ if (self.uninitialized) { #{::Kernel.raise ::TypeError, 'uninitialized Regexp'} } var result = 0; // should be supported in IE6 according to https://msdn.microsoft.com/en-us/library/7f5z26w4(v=vs.94).aspx if (self.multiline) { result |= #{MULTILINE}; } if (self.ignoreCase) { result |= #{IGNORECASE}; } return result; } end |
#source ⇒ Object Also known as: to_s
281 282 283 |
# File 'opal/opal/corelib/regexp.rb', line 281 def source `self.source` end |
#~ ⇒ Object
277 278 279 |
# File 'opal/opal/corelib/regexp.rb', line 277 def ~ self =~ $_ end |