Как извлечь строку перед совпадением с регулярным выражением? - PullRequest
2 голосов
/ 12 марта 2020

я пытаюсь извлечь shop title и address перед address regex match. Я застрял на некоторое время. Спасибо! В конце концов я тоже хочу получить телефон, но не знаю как. Может кто-то помочь мне с этим?

Функция:

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!');
  }

}

Текст:

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

Окончательный вывод:

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 Ответ

0 голосов
/ 12 марта 2020

Используйте только один шаблон: захватите заголовок в первой группе захвата (все на его строке), а на следующей строке, захватите ди git в начале строки, за которым следует Singapore, с

(.+)\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);
}

Или без 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);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...