I need a function to return all matches of a regexp in a string and positions at which the matches are found (I want to highlight matches in the string).
There
I think at least you can enhance your code a bit:
matches = [] while(match = str.match(regexp)) matches << match str = match.post_match end