How to get possibly overlapping matches in a string

后端 未结 8 2143
别那么骄傲
别那么骄傲 2020-12-03 17:13

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

8条回答
  •  独厮守ぢ
    2020-12-03 17:31

    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(" ")

提交回复
热议问题