Reputation: 8610
I'm looking for a way to find all overlapping regex hits in a given string when the regex is a combo of simple strings.
For example I'd like to get the following matches when searching for aaa|aaag:
aaaaggg //original string to search
aaa //match1 index=0
aaa //match2 index=1
aaag //match3 index=1
And one more example when searching for tt|aaa|aaag:
ttttaaaaggg //original string to search
tt //match0 index=0
tt //match1 index=1
tt //match2 index=2
aaa //match3 index=3
aaa //match4 index=4
aaag //match5 index=4
A javascript implementation preferred. Thank you!
EDIT: This is different than the suggested duplicate answer (Javascript Regex - Find all possible matches, even in already captured matches) because I want to detect overlaps with the SAME index. Here's a demo of the suggested solution NOT working here: https://jsbin.com/moferipibi/edit?html,js,console,output
Upvotes: 0
Views: 530
Reputation: 87
this does what you want :
let word = 'ttttaaaaggg';
let patterns = [/tt/g, /aaa/g, /aaaag/g]
function overlappingMatches(word, patterns) {
let matches = [];
let cpy, match;
patterns.map((pattern) => {
let cpy = word
while (cpy) {
match = [...cpy.matchAll(pattern)][0]
if (!match) break
matches.push(match[0])
cpy = cpy.substring(match.index + 1, )
}
})
return matches;
}
console.log(overlappingMatches(word, patterns))
it's clearly ugly but I didn't manage to get the overlapping matches without actually looping over the word. If you use an OR regex it stops at the first find, and the matchAll built in behaves the same.
Upvotes: 1