How to extract a line before a regex match?

Viewed 333

im trying to extract the shop title and address before an address regex match. I've been stuck for a while. Thanks! Eventually I want to get the telephone as well but im not sure how. Can someone help me with this?

Function:

function getShopInfo(text) {

const addrRegex = /[0-9]+.+(Singapore\s+\d{6}|S\d{6})/g;
const titleRegex = `/(.*\n){1}${addrRegex}/`; // extract the title before the address
if (text.match(titleRegex)) {
    const title = text.match(titleRegex);
    console.log(title);
    console.log(addr);
    console.log("\n")
  } else {
    console.log('Message: No TITLE before ADDRESS found!');
  }

}

Text:

LOCATE US
Din Tai Fung (Singapore, Thailand and United Kingdom) is managed by Taster Food Pte Ltd.
Din Tai Fung Singapore: We regret to inform that online/email reservations are not accepted. Please 
Name
Contact
Email
Subject
Restaurant
Message
SINGAPORE RESTAURANTS TEL
Paragon
290 Orchard Road #B1-03 Singapore 238859+65 6836 8336
BreadTalk IHQ
30 Tai Seng Street #01-02 Singapore 534013
+65 6702 0060
City Square Mall
180 Kitchener Road #01-10 Singapore 208539
+ 65 6634 2322
Chinatown Point
133 New Bridge Road #02-01 Singapore 059413
+65 6534 2722

Final Output:

Paragon
290 Orchard Road #B1-03 Singapore 238859

BreadTalk IHQ
30 Tai Seng Street #01-02 Singapore 534013

City Square Mall
180 Kitchener Road #01-10 Singapore 208539

Chinatown Point
133 New Bridge Road #02-01 Singapore 059413
1 Answers

Use just a single pattern: capture the title in the first capture group (everything on its line), and on the next line, capture a digit at the beginning of the line, eventually followed by Singapore, with

(.+)\n(\d+.*Singapore \d{6}\b)

https://regex101.com/r/VEKqjw/1

const input = `LOCATE US
Din Tai Fung (Singapore, Thailand and United Kingdom) is managed by Taster Food Pte Ltd.
Din Tai Fung Singapore: We regret to inform that online/email reservations are not accepted. Please 
Name
Contact
Email
Subject
Restaurant
Message
SINGAPORE RESTAURANTS TEL
Paragon
290 Orchard Road #B1-03 Singapore 238859+65 6836 8336
BreadTalk IHQ
30 Tai Seng Street #01-02 Singapore 534013
+65 6702 0060
City Square Mall
180 Kitchener Road #01-10 Singapore 208539
+ 65 6634 2322
Chinatown Point
133 New Bridge Road #02-01 Singapore 059413
+65 6534 2722`;
for (const [, title, address] of input.matchAll(/(.+)\n(\d+.*Singapore \d{6}\b)/g)) {
  console.log(title)
  console.log(address);
}

Or, without matchAll:

const input = `LOCATE US
Din Tai Fung (Singapore, Thailand and United Kingdom) is managed by Taster Food Pte Ltd.
Din Tai Fung Singapore: We regret to inform that online/email reservations are not accepted. Please 
Name
Contact
Email
Subject
Restaurant
Message
SINGAPORE RESTAURANTS TEL
Paragon
290 Orchard Road #B1-03 Singapore 238859+65 6836 8336
BreadTalk IHQ
30 Tai Seng Street #01-02 Singapore 534013
+65 6702 0060
City Square Mall
180 Kitchener Road #01-10 Singapore 208539
+ 65 6634 2322
Chinatown Point
133 New Bridge Road #02-01 Singapore 059413
+65 6534 2722`;
const pattern = /(.+)\n(\d+.*Singapore \d{6}\b)/g;
let match;
while (match = pattern.exec(input)) {
  const [, title, address] = match;
  console.log(title)
  console.log(address);
}

Related