grep :: Enumerable

grep(regex[, iterator = Prototype.K]) -> Array

 

Returns all the elements whose string representations match the regular expression. If an iterator is provided, it is used to produce the string representation for each selected element.

 

This is a variant of findAll, which is specific to pattern-matching String representations of the elements. It is mostly useful on sequences of Strings, obviously, but also on any objects with a toString method that fits such a usage.

 

Examples

 

// Get all strings with a repeated letter somewhere

['hello''world''this''is''cool'].grep(/(.)\1/)

// -> ['hello', 'cool']

 

$('myTable').descendants().grep(/t[dh]/, function(node) {

  return node.tagName.toLowerCase();

})

// -> only td/th elements inside the table

 

// Get all numbers ending with 0 or 5

$R(1,30).grep(/[05]$/)

// -> [5, 10, 15, 20, 25, 30]

 

 


Prototype API 1.5.0 - prototypejs.org