const regex = /car (zap(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|yes(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|xerox(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|with(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|view(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|under(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|this(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|sort(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|rest(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|quick|(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|orange(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|nope|(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|letter(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|kayak(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|joseph(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|indiana(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|humus(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|gelatin(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|frankfurter(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|elephant(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|doormat(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|carrot(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|banana(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|apple(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?) crashed/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('car (zap(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|yes(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|xerox(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|with(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|view(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|under(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|this(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|sort(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|rest(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|quick|(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|orange(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|nope|(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|letter(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|kayak(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|joseph(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|indiana(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|humus(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|gelatin(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|frankfurter(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|elephant(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|doormat(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|carrot(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|banana(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|apple(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?) crashed', 'g')
const str = `car apple view crashed
car doormat elephant crashed
car humus zap crashed
car banana crashed
car crashedddd
^ the above shouldn't matchd`;
// 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