const regex = /\$\s*(?<text>[^$]+?)\s*\$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('\\$\\s*(?<text>[^$]+?)\\s*\\$', 'gm')
const str = `\$Hayden Smith\$
\$Previous work history: 7
Communication Skills: 8
Resilience:7\$
\$Park Hill Soccer Club Canteen - Park Hill Secondary College- 3 months - Volunteer
Argo Newsagency - Newspaper deliverer - 8 months\$
\$Hayden is a reliable and hard-working year 11 student seeking customer service work. He has displayed good communication skills, numeracy skills for cash handling, and strong ability to work in a team. He has gained customer service experience while volunteering at the soccer club canteen, as well as experience in handling cash as a newspaper deliverer. Hayden has also demonstrated leadership skills through his roles as a soccer umpire and assistant coach. In his spare time, he enjoys playing and watching soccer, football, and cricket. \$`;
// 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