const regex = /(?:(?:[一-龥,、‘’《》]|(.*?)|\(.*?\))+[,。;?!])/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?:(?:[一-龥,、‘’《》]|(.*?)|\\(.*?\\))+[,。;?!])', 'g')
const str = `
# 2、鱼我所欲也(《孟子·告子上》)
鱼,我所欲也,熊掌,亦我所欲也,二者不可得兼,舍鱼而取熊掌者也。生,亦我所欲也,义,亦我所欲也,二者不可得兼,舍生而取义者也。生亦我所欲,所欲有甚于生者,故不为苟得也。死亦我所恶,所恶有甚于死者,故患有所不辟也。如使人之所欲莫甚于生,则凡可以得生者何不用也。使人之所恶莫甚于死者,则凡可以辟患者何不为也!由是则生而有不用也,由是则可以辟患而有不为也。是故所欲有甚于生者,所恶有甚于死者。非独贤者有是心也,人皆有之,贤者能勿丧耳。
一箪食,一豆羹,得之则生,弗得则死。呼尔而与之,行道之人弗受;蹴尔而与之,乞人不屑也。
万钟则不辩礼义而受之,万钟于我何加焉!为宫室之美,妻妾之奉,所识穷乏者得我与?乡为身死而不受,今为宫室之美为之;乡为身死而不受,今为妻妾之奉为之;乡为身死而不受,今为所识穷乏者得我而为之;是亦不可以已乎?此之谓失其本心。
`;
// 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