Is there a function like String#scan, but returning array of MatchDatas?

前端 未结 4 1380
名媛妹妹
名媛妹妹 2021-02-04 18:18

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

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-04 18:35

    I think at least you can enhance your code a bit:

    matches = []
    while(match = str.match(regexp)) 
      matches << match
      str = match.post_match
    end
    

提交回复
热议问题