const regex = /(t(?:h[uứ])? ?(?:tư|[2-7]|hai|ba|năm|sáu|bảy)|chủ nhật)( *)(?: ?(?:đầu|cuối)? ?(?:tuần (?:trước|sau|này)))?$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(t(?:h[uứ])? ?(?:tư|[2-7]|hai|ba|năm|sáu|bảy)|chủ nhật)( *)(?: ?(?:đầu|cuối)? ?(?:tuần (?:trước|sau|này)))?$', 'gm')
const str = `thứ 2
thứ 3
thứ 4
thứ 5
thứ 6
thứ 7
t2
t3
t4
t5
t6
t7
thứ hai
thứ ba
thứ tư
thứ2
thứ3
chủnhật
Chủ nhật
thứ hai cuối tuần sau
thứ ba cuối tuần sau
thứ tư cuối tuần sau
thứ năm cuối tuần sau
thứ năm đầu tuần sau
thứ sáu đầu tuần sau
thứ bảy tuần sau
chủ nhật tuần trước
chủ nhật tuần này
t8 cuối tuần này
thuhai
thuba
thutu
thunam
tha thu
`;
// 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