Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在匹配之前提取所有单词?_Javascript_Regex - Fatal编程技术网

Javascript 如何在匹配之前提取所有单词?

Javascript 如何在匹配之前提取所有单词?,javascript,regex,Javascript,Regex,是否有任何正则表达式将标题和地址从文本中分离到下面的输出 这就是我目前的情况: .+?(?=\d+.*Singapore \d{6}\b) Marina Bay Sands Relocated! 2 Bayfront Avenue Galleria Level #B1-01 Singapore 018972 +65 6634 9969 nex 23 Serangoon Central #B1-10 Singapore 556083 +65 6634 7787 Northpoint City 1

是否有任何正则表达式将
标题
地址
从文本中分离到下面的输出

这就是我目前的情况:

.+?(?=\d+.*Singapore \d{6}\b)
Marina Bay Sands Relocated! 2 Bayfront Avenue Galleria Level #B1-01 Singapore 018972
+65 6634 9969
nex 23 Serangoon Central #B1-10 Singapore 556083
+65 6634 7787
Northpoint City 1 Northpoint Drive South Wing #B1-107 Singapore 768019
+65 6481 3433
Marina Bay Sands Relocated! 
2 Bayfront Avenue Galleria Level #B1-01 Singapore 018972

nex 
23 Serangoon Central #B1-10 Singapore 556083

Northpoint City 1 Northpoint Drive South Wing #B1-107 Singapore 768019
+65 6481 3433
文本:

.+?(?=\d+.*Singapore \d{6}\b)
Marina Bay Sands Relocated! 2 Bayfront Avenue Galleria Level #B1-01 Singapore 018972
+65 6634 9969
nex 23 Serangoon Central #B1-10 Singapore 556083
+65 6634 7787
Northpoint City 1 Northpoint Drive South Wing #B1-107 Singapore 768019
+65 6481 3433
Marina Bay Sands Relocated! 
2 Bayfront Avenue Galleria Level #B1-01 Singapore 018972

nex 
23 Serangoon Central #B1-10 Singapore 556083

Northpoint City 1 Northpoint Drive South Wing #B1-107 Singapore 768019
+65 6481 3433
输出:

.+?(?=\d+.*Singapore \d{6}\b)
Marina Bay Sands Relocated! 2 Bayfront Avenue Galleria Level #B1-01 Singapore 018972
+65 6634 9969
nex 23 Serangoon Central #B1-10 Singapore 556083
+65 6634 7787
Northpoint City 1 Northpoint Drive South Wing #B1-107 Singapore 768019
+65 6481 3433
Marina Bay Sands Relocated! 
2 Bayfront Avenue Galleria Level #B1-01 Singapore 018972

nex 
23 Serangoon Central #B1-10 Singapore 556083

Northpoint City 1 Northpoint Drive South Wing #B1-107 Singapore 768019
+65 6481 3433
你可以用

(.+?)\s*(\d+.*Singapore \d{6})\b(?:\r?\n(\+65\s*\d{4}\s*\d{4}))?
或者只是

(.+?)\s*(\d+.*Singapore \d{6})\b(?:\r?\n(\+65[\d ]*))?

详细信息

  • (.+?)
    -第1组:除换行符以外的任何1个或多个字符,尽可能少
  • \s*
    -0+空格
  • (\d+.*Singapore\d{6})
    -第2组:1+位,除换行符以外的任何0+字符,尽可能多,
    Singapore
    ,然后是6位数字
  • \b
    -单词边界
  • (?:\r?\n(\+65\s*\d{4}\s*\d{4}))?
    -一个可选的
    • \r?\n
      -CRLF或LF行结束
    • (\+65\s*\d{4}\s*\d{4})
      -第3组:
      +65
      ,0+空格,4位数字,0+空格,4位数字。
      [\d]*
      将匹配0个或更多数字或空格
每场比赛三组内容:

Marina Bay Sands Relocated!
2 Bayfront Avenue Galleria Level #B1-01 Singapore 018972
+65 6634 9969

nex
23 Serangoon Central #B1-10 Singapore 556083
+65 6634 7787

Northpoint City
1 Northpoint Drive South Wing #B1-107 Singapore 768019
+65 6481 3433

可能是
/(.+?)\s*(\d++.*Singapore\d{6})\b/
?看见电话号码不应该出现在比赛中,对吗?如果可能的话我也想比赛谢谢(:Like?or?regex101.com/r/i0G0wH/2我认为这应该适合我的需要。但是,regex似乎也匹配完整的地址和电话?我希望它分开。谢谢你的帮助!很好的解释。我将对此竖起大拇指!!@Wiktor StribizewStribizew我已经给了你一票。再次感谢你的帮助。干杯!