const regex = /Hometown: ([^<]+)(?=\s*<)/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('Hometown: ([^<]+)(?=\\s*<)', 'g')
const str = `<strong><a href="/search/company/company/94613582">Anchor sample Ltd</a></strong><br>
BIS: 94613582 <br>
Hometown: MONTREAL <br>
<div class="hori"></div>
<strong><a href="/search/company/company/046251945">Anchor sample Ltd</a></strong><br>
BIS: 046251945 <br>
Hometown: ALABAMA <br>
<div class="hori"></div>
<strong><a href="/search/company/company/041634545">Anchor sample Ltd</a></strong><br>
BIS: 041634545 <br>
Hometown: GEORGIA <br>
<div class="hori"></div>
<strong><a href="/search/company/company/487915646">Anchor sample Ltd</a></strong><br>
BIS: 487915646 <br>
Hometown: FLORIDA <br>
<div class="hori"></div>
<strong><a href="/search/company/company/165875487">Anchor sample Ltd</a></strong><br>
BIS: 165875487 <br>
Hometown: KANSAS <br>`;
// 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