Class: RubyJS.MatchData

Defined in: lib/corelib/match_data.coffee
Inherits: RubyJS.Object

Overview

TODO: MatchData only works properly when calling through Regexp#match.

Is a wrapper around Regexp returns that acts like Rubys MatchData. This is only partially complete due to missing features in JS Regexp.

String#scan, #match to be done.

Instance Method Summary

Inherited Method Summary

Methods inherited from RubyJS.Object

.include, .__add_default_aliases__, #send, #respond_to, #to_enum, #tap

Constructor Details

- (void) constructor(__native__, opts = {})

Instance Method Details

- (void) is_match_data()

- (void) '=='(other)

- (void) begin(offset)

Returns the offset of the start of the nth element of the match array in the string. n can be a string or symbol to reference a named capture.

Examples:


  m = R(/(.)(.)(\d+)(\d)/).match("THX1138.")
  m.begin(0)       #=> 1
  m.begin(2)       #=> 2

unsupported Ruby syntax


  # m = R/(?<foo>.)(.)(?<bar>.)/.match("hoge")
  # m.begin(:foo)  #=> 0
  # m.begin(:bar)  #=> 2

- (void) captures()

Returns the array of captures; equivalent to mtch.to_a[1..-1].

Examples:


  capt = R(/(.)(.)(\d+)(\d)/).match("THX1138.").captures()
  capt[0]    #=> "H"
  capt[1]    #=> "X"
  capt[2]    #=> "113"
  capt[3]    #=> "8"

Additional examples, edge cases


  R(/foo/).match("foo").captures() #=> []

- (void) end(offset)

- (void) eql(other)

See also:

  • #==

- (void) get(args...)

Match Reference—MatchData acts as an array, and may be accessed using the normal array indexing techniques. mtch is equivalent to the special variable $&, and returns the entire matched string. mtch, mtch, and so on return the values of the matched backreferences (portions of the pattern between parentheses).

Examples:

  m = R(/(.)(.)(\d+)(\d)/).match("THX1138.")
  m          #=> #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8">
  m.get(0)       #=> "HX1138"
  m.get(1, 2)    #=> ["H", "X"]
  # m.get(1..3)    #=> ["H", "X", "113"]
  m.get(-3, 2)   #=> ["X", "113"]

accessing with [] is supported only for single arguments.

  m[0]           #=> "HX1138"

Unsupported Ruby Syntax (named captures)

  # m = /(?<foo>a+)b/.match("ccaaab")
  # m          #=> #<MatchData "aaab" foo:"aaa">
  # m["foo"]   #=> "aaa"
  # m[:foo]    #=> "aaa"

Parameters and return types

  mtch[i] → str or nil
  mtch[start, length] → array
  mtch[range] → array
  # mtch[name] → str or nil

- (void) inspect()

Returns a printable version of mtch.

Examples:


  puts /.$/.match("foo").inspect()
  #=> #<MatchData "o">

  puts /(.)(.)(.)/.match("foo").inspect()
  #=> #<MatchData "foo" 1:"f" 2:"o" 3:"o">

  puts /(.)(.)?(.)/.match("fo").inspect()
  #=> #<MatchData "fo" 1:"f" 2:nil 3:"o">

Unsupported Ruby Syntax


  puts /(?<foo>.)(?<bar>.)(?<baz>.)/.match("hoge").inspect()
  #=> #<MatchData "hog" foo:"h" bar:"o" baz:"g">

- (void) length()

Returns the number of elements in the match array.

@alias #size()

Examples:

  m = R(/(.)(.)(\d+)(\d)/).match("THX1138.")
  m.length()   #=> 5
  m.size()     #=> 5

- (void) offset(offset)

TODO: check types

- (void) post_match()

- (void) pre_match()

- (void) regexp()

- (void) size()

@alias #length()

See also:

- (void) string()

- (void) to_a()

Returns the array of matches.

m = R(/(.)(.)(\d+)(\d)/).match("THX1138.")
m.to_a()   #=> ["HX1138", "H", "X", "113", "8"]

Because to_a is called when expanding *variable, there’s a useful assignment shortcut for extracting matched fields. This is slightly slower than accessing the fields directly (as an intermediate array is generated).

- (void) to_s()

Returns the entire matched string.

Examples:

  m = R(/(.)(.)(\d+)(\d)/).match("THX1138.")
  m.to_s()   #=> "HX1138"

- (void) values_at(indices...)

values_at([index]*) → array click to toggle source Uses each index to access the matching values, returning an array of the corresponding matches.

Examples:

  m = R(/(.)(.)(\d+)(\d)/).match("THX1138: The Movie")
  m.to_a()                #=> ["HX1138", "H", "X", "113", "8"]
  m.values_at(0, 2, -2)   #=> ["HX1138", "X", "113"]

- (void) names()

@unsupported extracting names is not supported in Javascript