const regex = /^(?<weeks>\d{1,2})w\s(?<days>0?[0-6])d\s(?<hours>(?:0?|1|2(?=[0-3]))\d)h\s(?<minutes>(?:0?|[1-5])\d)m$/i;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?<weeks>\\d{1,2})w\\s(?<days>0?[0-6])d\\s(?<hours>(?:0?|1|2(?=[0-3]))\\d)h\\s(?<minutes>(?:0?|[1-5])\\d)m$', 'i')
const str = `00w 00d 21h 00m`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
if ((m = regex.exec(str)) !== null) {
// 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