Search code examples
javascriptregex

Regex - Strip out commas and spaces from 'empty' address fields


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.


Solution

  • 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, ''));
    

    Consecutive commas with spaces regex pattern diagram

    Consecutive commas with spaces regex pattern explanation


    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, ''));
    

    All consecutive commas regex pattern diagram

    All consecutive commas regex pattern explanation