简体   繁体   中英

Regex: how to extract text until end of string with multiple new lines?

I want it to match each of the text and flexible enough to read multiple lines instead of a single line.

Text single input example

383 Bukit Timah Road #01-09B Alocassia Apartments Singapore 259727

Text with multiple lines example

383 Bukit Timah Road 
#01-09B Alocassia Apartments 
Singapore 259727

448 Ang Mo Kio 
Avenue 10 #01-1693 
Singapore 560448

383 Bukit Timah Road 
#01-09B Alocassia Apartments 
Singapore 259727

Regex:

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

What you're looking for is the s flag (single line: dot matches newline). https://regex101.com/r/ZTP1hC/1

Use [^] (specific to JavaScript Regex) to match any character including linebreaks

The regex is:

^(\d+ [^]*?\b\d{6})$

Demo & explanation

 var test = [ `383 Bukit Timah Road #01-09B Alocassia Apartments Singapore 259727`, `383 Bukit Timah Road #01-09B Alocassia Apartments Singapore 259727`, `448 Ang Mo Kio Avenue 10 #01-1693 Singapore 560448`, `383 Bukit Timah Road #01-09B Alocassia Apartments Singapore 259727`, `123 blah blah`, `blah blah 123456`, `123 blah blah 1234567` ]; console.log(test.map(function (a) { return a + ' :' + /^(\\d+ [^]*?\\b\\d{6})$/.test(a); }));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM