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
Approach of RegExp
/(a.c)|(a.*c)/g
is to match "a"
character followed by any character followed by "c"
; "a.*c"
is to match "a"
followed by any character followed by preceding character followed by "c"
character ; note RegExp
at (a.*c)
could probably be improved. Condition at if
checks if last character in input string is "c"
, if true
, push full input string to res
results array
var str = "abcadc"
, res = str.match(/(a.c)|(a.*c)/g);
if (str[str.length - 1] === "c") res.push(str);
document.body.textContent = res.join(" ")