const regex = /^([^:(,]*)\b/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^([^:(,]*)\\b', 'gm')
const str = `Boston (Boston University, Boston College, Bos...
Bridgewater (Bridgewater State College)[2]
Cambridge (Harvard University, Massachusetts I...
Chestnut Hill (Boston College)
The Colleges of Worcester Consortium:
Dudley (Nichols College)
Faribault, South Central College
Mankato (Minnesota State University, Mankato),...
Marshall (Southwest Minnesota State University...
Moorhead (Minnesota State University, Moorhead...
Morris (University of Minnesota Morris)[2]
Northfield (Carleton College, St. Olaf College...
North Mankato, South Central College
St. Cloud (St. Cloud State University, The Col...
St. Joseph (College of Saint Benedict)[2]
St. Peter (Gustavus Adolphus College)[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