1

I want all data of Hometown:
How do i enchance this regex

Hometown: ([^<]*) <br>

to get all the Hometown: fields.
Currently it will take first and stops in there, wont continue to next Hometown: field

Some sample inputs:

    <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>
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
twisters
  • 11
  • 1

2 Answers2

1

Here you are, you need to find the string "Hometown", then capture the name itself into a capturing group, and then only use this first group and to match them all you should use /g modifier with exec method:

var re = /Hometown: ([^<]+)\s/g; 
var str = '<strong><a href="/search/company/company/94613582">Anchor sample Ltd</a></strong><br>\n    BIS: 94613582 <br>\n    Hometown: MONTREAL <br>\n\n\n    <div class="hori"></div>\n\n    <strong><a href="/search/company/company/046251945">Anchor sample Ltd</a></strong><br>\n    BIS: 046251945 <br>\n    Hometown: ALABAMA <br>\n\n\n    <div class="hori"></div>\n\n    <strong><a href="/search/company/company/041634545">Anchor sample Ltd</a></strong><br>\n    BIS: 041634545 <br>\n    Hometown: GEORGIA <br>\n\n\n    <div class="hori"></div>\n\n    <strong><a href="/search/company/company/487915646">Anchor sample Ltd</a></strong><br>\n    BIS: 487915646 <br>\n    Hometown: FLORIDA <br>\n\n\n    <div class="hori"></div>\n\n    <strong><a href="/search/company/company/165875487">Anchor sample Ltd</a></strong><br>\n    BIS: 165875487 <br>\n    Hometown: KANSAS <br>';
var m;
 
while ((m = re.exec(str)) !== null) {
    document.getElementById("res").innerHTML += "<br>" + m[1];
}
<div id="res"/>

In case the space is optional after the city name, use

var re = /Hometown: ([^<]+)(?=\s*<)/g;

See demo

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

'g' modifier should be set.

The g modifier is used to perform a global match (find all matches rather than stopping after the first match).

Dmitry
  • 281
  • 6
  • 13