How can I match overlapping strings with regex?

2018-12-31 18:06发布

问题:

Let\'s say I have the string

\"12345\"

If I .match(/\\d{3}/g), I only get one match, \"123\". Why don\'t I get [ \"123\", \"234\", \"345\" ]?

回答1:

You can\'t do this with a regex alone, but you can get pretty close:

var pat = /(?=(\\d{3}))\\d/g;
var results = [];
var match;

while ( (match = pat.exec( \'1234567\' ) ) != null ) { 
  results.push( match[1] );
}

console.log(results);

In other words, you capture all three digits inside the lookahead, then go back and match one character in the normal way just to advance the match position. It doesn\'t matter how you consume that character; . works just as well \\d. And if you\'re really feeling adventurous, you can use just the lookahead and let JavaScript handle the bump-along.

This code is adapted from this answer. I would have flagged this question as a duplicate of that one, but the OP accepted another, lesser answer.



回答2:

The string#match with a global flag regex returns an array of matched substrings. The /\\d{3}/g regex matches and consumes (=reads into the buffer and advances its index to the position right after the currently matched character) 3 digit sequence. Thus, after \"eating up\" 123, the index is located after 3, and the only substring left for parsing is 45 - no match here.

I think the technique used at regex101.com is also worth considering here: use a zero-width assertion (a positive lookahead with a capturing group) to test all positions inside the input string. After each test, the RegExp.lastIndex (it\'s a read/write integer property of regular expressions that specifies the index at which to start the next match) is advanced \"manually\" to avoid infinite loop.

Note it is a technique implemented in .NET (Regex.Matches), Python (re.findall), PHP (preg_match_all).

Here is a demo:

var re = /(?=(\\d{3}))/g; 
var str = \'12345\';
var res = [];
var m;
 
while ((m = re.exec(str)) !== null) {
    if (m.index === re.lastIndex) {
        re.lastIndex++;
    }
    res.push(m[1]);
}

document.body.innerHTML = JSON.stringify(res);

Here is a regex101.com demo



回答3:

When an expression matches, it usually consumes the characters it matched. So, after the expression matched 123, only 45 is left, which doesn\'t match the pattern.



回答4:

To answer the \"How\", you can manually change the index of the last match (requires a loop) :

var input = \'12345\', 
    re = /\\d{3}/g, 
    r = [], 
    m;
while (m = re.exec(input)) {
    re.lastIndex -= m[0].length - 1;
    r.push(m[0]);
}
r; // [\"123\", \"234\", \"345\"]

Here is a function for convenience :

function matchOverlap(input, re) {
    var r = [], m;
    // prevent infinite loops
    if (!re.global) re = new RegExp(
        re.source, (re+\'\').split(\'/\').pop() + \'g\'
    );
    while (m = re.exec(input)) {
        re.lastIndex -= m[0].length - 1;
        r.push(m[0]);
    }
    return r;
}

Usage examples :

matchOverlap(\'12345\', /\\D{3}/)      // []
matchOverlap(\'12345\', /\\d{3}/)      // [\"123\", \"234\", \"345\"]
matchOverlap(\'12345\', /\\d{3}/g)     // [\"123\", \"234\", \"345\"]
matchOverlap(\'1234 5678\', /\\d{3}/)  // [\"123\", \"234\", \"567\", \"678\"]
matchOverlap(\'LOLOL\', /lol/)        // []
matchOverlap(\'LOLOL\', /lol/i)       // [\"LOL\", \"LOL\"]


回答5:

Use (?=(\\w{3}))

(3 being the number of letters in the sequence)



回答6:

I would consider not using a regex for this. If you want to split into groups of three you can just loop over the string starting at the offset:

let s = \"12345\"
let m = Array.from(s.slice(2), (_, i) => s.slice(i, i+3))
console.log(m)