I am using a third party address lookup service that provides address data in the following format:
'addr line one, , , , city, county, postcode, addr line one, addr line two, , , city, county, postcode, addr line one, , , , city, county, postcode, addr line one, addr line two, addr line three, addr line four, city, county, postcode'
As you can see from the data, often there are 'empty' fields that are still delimited by a comma followed by a space.
The input string is split into individual address records:
'addr line one, , , , city, county, postcode'
'addr line one, addr line two, , , city, county, postcode'
'addr line one, , , , city, county, postcode'
'addr line one, addr line two, addr line three, addr line four, city, county, postcode'
I am looking to produce the following output:
'addr line one, city, county, postcode'
'addr line one, addr line two, city, county, postcode'
'addr line one, city, county, postcode'
'addr line one, addr line two, addr line three, addr line four, city, county, postcode'
I would imagine that a regex would be a good solution, but I am at a loss as to how to create a regex that would filter this string correctly.
Either of these two regex patterns should clean up 'empty' fields in address data:
1. Remove consecutive comma-space(s) pattern
\s+
→ , , , , , ,
\s+
→ , , , , , ,
const address = 'addr line one, , , , city, county, postcode, ...';
const commaSpaceRegex = /(,\s+(?=,))+/g;
console.log(address.replace(commaSpaceRegex, ''));
2. Remove all consecutive commas - with or without space(s)
\s*
→ , , , , , ,
\s*
→ ,,,,,,
\s*
→ ,, , , ,,
const address = 'addr line one, , , , city, county, postcode, ...';
const allCommasRegex = /(,\s*(?=,))+/g;
console.log(address.replace(allCommasRegex, ''));