Javascript 如何在正则表达式匹配之前提取行?

Javascript 如何在正则表达式匹配之前提取行?,javascript,regex,Javascript,Regex,我试图在地址正则表达式匹配之前提取店铺名称和地址。我被困了一段时间。谢谢最终我也想得到电话,但我不知道如何得到。有人能帮我吗 功能: 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.m

我试图在
地址正则表达式匹配之前提取
店铺名称
地址
。我被困了一段时间。谢谢最终我也想得到电话,但我不知道如何得到。有人能帮我吗

功能:

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
文本:

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
最终输出:

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

仅使用一种模式:在第一个捕获组中捕获标题(其行中的所有内容),在下一行中,在行的开头捕获一个数字,最后在
新加坡
之后,使用

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

const input=`找到我们
鼎泰丰(新加坡、泰国和英国)由Taster Food Pte Ltd.管理。
新加坡鼎泰丰:我们遗憾地通知您,我们不接受在线/电子邮件预订。请
名称
接触
电子邮件
主题
餐厅
消息
新加坡餐厅电话
典范
新加坡乌节路290号#B1-03 238859+65 6836 8336
面包语IHQ
新加坡泰生街30号#01-02 534013
+65 6702 0060
城市广场购物中心
新加坡基奇纳路180号#01-10 208539
+ 65 6634 2322
唐城坊
新桥路133号#02-01新加坡059413
+65 6534 2722`;
for(input.matchAll(/(.+)\n(\d++.*Singapore\d{6}\b)/g)的常量[,标题,地址]){
console.log(标题)
控制台日志(地址);

}
到目前为止,我将对测试用例使用以下正则表达式
(.*)\n(\d+.*Singapore\s+\d{6})
,它假设地址只有一行,并且标题(或非地址行)显示在地址之前。如果你想要更好的东西,你应该包括更多的测试用例,感谢@CertainPerformance的快速响应。我可以知道while循环中的[名称,地址]是什么吗?我没有得到第一个逗号,这是解构。它从匹配数组中获取
[1]
[2]
标记,并将它们放入两个变量名
title
address
(同时丢弃
[0]
索引-这就是为什么在第一个逗号之前没有任何标记)您将其说得非常清楚。我接受这个答案。谢谢@CertainPerformance