const regex = /(S[0-9]+-[0-9]+-[0-9]+|S[0-9]+)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(S[0-9]+-[0-9]+-[0-9]+|S[0-9]+)', 'gm')
const str = `Samples
101003 W1D1 Pre
101003 W1D1 4H
101003 W1D2 Pre
101003 W2D1 Pre
101004 W1D1 Pre
101004 W1D1 4H
101004 W1D2 Pre
101004 W2D1 Pre
101005 W1D1 Pre
101005 W1D1 4H
101005 W1D2 Pre
101005 W2D1 Pre
101007 W1D1 Pre
101007 W1D1 4H
101007 W1D2 Pre
101007 W2D1 Pre
102001 W1D1 Pre
102001 W1D1 4H
102001 W1D2 Pre
102001 W2D1 Pre
102002 W1D1 Pre
102002 W1D1 4H
102002 W1D2 Pre
102002 W2D1 Pre
102005 W1D1 Pre
102005 W1D1 4H
102005 W1D2 Pre
102005 W2D1 Pre
104001 W1D1 Pre
104001 W1D1 4H
104001 W1D2 Pre
104001 W2D1 Pre
104002 W1D1 Pre
104002 W1D1 4H
104002 W1D2 Pre
104002 W2D1 Pre
104005 W1D1 Pre
104005 W1D1 4H
104005 W1D2 Pre
104005 W2D1 Pre
105001 W1D1 Pre
105001 W1D1 4H
105001 W1D2 Pre
105001 W2D1 Pre
105002 W1D1 Pre
105002 W1D1 4H
105002 W1D2 Pre
105002 W2D1 Pre
105005 W1D1 Pre
105005 W1D1 4H
105005 W1D2 Pre
105005 W2D1 Pre
`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
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}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions