const regex = /^.{0,1}(陪我一起)?听?你?(再|快|先)?能?(给我)?唱?(啊|一?个|吧|出来|一?首)?歌?(给我听|来听听)?(吧|啊|哇|呗|呀)?((好(么|嘛|吗))|好不好|小万|亲爱的|这么难)?.{0,1}$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^.{0,1}(陪我一起)?听?你?(再|快|先)?能?(给我)?唱?(啊|一?个|吧|出来|一?首)?歌?(给我听|来听听)?(吧|啊|哇|呗|呀)?((好(么|嘛|吗))|好不好|小万|亲爱的|这么难)?.{0,1}$', 'gm')
const str = `你先唱给我听好吗
快唱啊
快唱啊你
你唱吧
你唱一个
唱一首吧亲爱的
再唱一首
你快唱吧
你能唱出来吗
唱出来
唱一个给我听呗好不好
快唱
唱
唱吧
你唱啊
唱一个嘛
唱一个啊
来听你唱歌
再给我唱一首呗
唱歌小万
你唱首歌给我听呗
陪我一起唱歌好吗
听你唱歌这么难
唱首歌来听听
你唱歌好好听啊再给我唱一首呗
我现在就很不开心你唱首歌给我听呗
会不会唱歌,唱首歌来听听`;
// 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