I'm trying to split street name, house number, and box number from a String.
Let's say the string is "SomeStreet 59A"
For this case I already have a solution with regex. I'm using this function:
address.split(/([0-9]+)/) //output ["SomeStreet","59","A"]
The problem I'm having now, is that some addresses have weird formats. Meaning, the above method does not fit for strings like:
"Somestreet 59-65" // output ["SomeStreet", "59", "-", "65"] Not good
My question for this case is, how to group the numbers to get this desired output:
["Somestreet", "59-65"]
Another weird example is:
"6' SomeStreet 59" // here "6' Somestreet" is the exact street-name.
Expected output: ["6' Somestreet", "59"]
"6' Somestreet 324/326 A/1" // Example with box number
Expected output: ["6' Somestreet", "324/326", "A/1"]
Bear in mind that this has to be in one executable function to loop through all of the addresses that I have.
.split(/\s*(\d+(?!['’\d])(?:-\d+)?)/)
(see demo) if all acceptable formats are those you listed in the question. – Technical