I\'m looking for a way, either in Ruby or Javascript, that will give me all matches, possibly overlapping, within a string against a regexp.
Let\'s say I have
▶ str = "abcadc"
▶ from = str.split(/(?=\p{L})/).map.with_index { |c, i| i if c == 'a' }.compact
▶ to = str.split(/(?=\p{L})/).map.with_index { |c, i| i if c == 'c' }.compact
▶ from.product(to).select { |f,t| f < t }.map { |f,t| str[f..t] }
#⇒ [
# [0] "abc",
# [1] "abcadc",
# [2] "adc"
# ]
I believe, that there is a fancy way to find all indices of a character in a string, but I was unable to find it :( Any ideas?
Splitting on “unicode char boundary” makes it to work with strings like 'ábĉ'
or 'Üve Østergaard'
.
For more generic solution, that accepts any “from” and “to” sequences, one should introduce just a little modification: find all indices of “from” and “to” in the string.