const regex = /^\s*(add|and|nor|or|sub)\s*(\$[a-z0-9]{2,4}),\s*(\$[a-z0-9]{2,4}),\s*(\$[a-z0-9]{2,4})\s*$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^\\s*(add|and|nor|or|sub)\\s*(\\$[a-z0-9]{2,4}),\\s*(\\$[a-z0-9]{2,4}),\\s*(\\$[a-z0-9]{2,4})\\s*$', 'gm')
const str = `add \$t3,\$s3,\$s5
add \$t3,\$s3,\$s5
add \$v0,\$t0,\$t0
add \$v0,\$t0,\$t0
and \$t2,\$t3,\$t4
and \$t2,\$t3,\$t4
nor \$s0,\$s5,\$s2
nor \$s0,\$s5,\$s2
or \$s0,\$s0,\$s1
or \$s0,\$s0,\$s1
or \$s0,\$s2,\$s5
or \$s0,\$s2,\$s5
sub \$s3,\$t0,\$t1
sub \$s3,\$t0,\$t1
sub \$t0,\$zero,\$t1
sub \$t0,\$zero,\$t1
sub \$t1,\$t4,\$t8
sub \$t1,\$t4,\$t8`;
// 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