const regex = /:\w+-\w+\K\s|\sNODE_|_length_|_cov_|,\s/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp(':\\w+-\\w+\\K\\s|\\sNODE_|_length_|_cov_|,\\s', 'gm')
const str = `NZ_MCQZ01000071.1:2282-2767 Klebsiella pneumoniae strain TR196 Scaffold45_1, whole genome shotgun sequence
RYOH01000117.1:3-590 Klebsiella pneumoniae strain 16WZ-131 NODE_117_length_2026_cov_233.332478, whole genome shotgun sequence
RYOJ01000145.1:3-857 Klebsiella pneumoniae strain 16WZ-128 NODE_145_length_2293_cov_224.091606, whole genome shotgun sequence
NZ_CABWRH010000049.1:1707-2128 Klebsiella pneumoniae strain SRRSH43 isolate SRRSH43, whole genome shotgun sequence
RYQS01000239.1:1916-2698 Klebsiella pneumoniae strain 16HN-12 NODE_239_length_2763_cov_7.539092, whole genome shotgun sequence`;
// 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