[Solved] What could be the regex for matching combination of a specific string followed by number in a range?


This expression matches only those desired iOS versions listed:

iOS[89]|iOS[1][0-3]

Demo

Test

const regex = /iOS[89]|iOS[1][0-3]/gm;
const str = `iOS7
iOS8
iOS9
iOS10
iOS11
iOS12
iOS13
iOS14`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

For iOS1 to iOS213, the following expression might for instance work, and we’d add start and end anchors, if we’d be validating:

^iOS[2][1][0-3]$|^iOS[2][0]\d$|^iOS[1][0-9][0-9]$|^iOS[1-9][0-9]$|^iOS[1-9]$

Demo 2

const regex = /^iOS[2][1][0-3]$|^iOS[2][0]\d$|^iOS[1][0-9][0-9]$|^iOS[1-9][0-9]$|^iOS[1-9]$/gm;
const str = `iOS0
iOS7
iOS8
iOS9
iOS10
iOS11
iOS12
iOS13
iOS14
iOS200
iOS201
iOS202
iOS203
iOS205
iOS214`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

1

solved What could be the regex for matching combination of a specific string followed by number in a range?