const regex = /(?:(:\n\s))(?:\s|\n|(?:.(?!modern))*)*/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?:(:\\n\\s))(?:\\s|\\n|(?:.(?!modern))*)*', 'gm')
const str = `js/modern/redux/actions/open-file-in-new-window.js:
8 import { openWindow, maybeSetVariantRedirectDomainQueryParam } from '@slack/infra/multi-window';
9: import getPrimaryView from '@slack/redux/stores/workspace-store/get-primary-view';
10 import { isGovSlack } from '@slack/utility/is-gov-slack';
26 const teamId = getTeamId(team);
27: const conversationId = getPrimaryView(state);
28 let fileUrl = teamId
js/modern/redux/actions/open-shortcuts-menu.js:
10 import looksLikeChannelId from '@slack/utility/looks-like-channel-id';
11: import getPrimaryView from '@slack/redux/stores/workspace-store/get-primary-view';
12 import jumpToMessage from '@slack/redux/actions/jump-to-message';
25 const state = getState();
26: const primaryViewId = getPrimaryView(state);
27 `;
// 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