StuBlackett
StuBlackett

Reputation: 3857

Strip a comma and whitespace thats after it from a string

I'm working with a Postcode Finder and some of the address elements i.e County are returning empty at times as a ", "

I'm looking to strip them from the string to return a more readable address format

Example Data is JSON :

{
"latitude": 53.381130218505859,
"longitude": -1.4663739204406738,
"addresses": [
    "Crucible Enterprises Ltd, 55 Norfolk Street, , , , Sheffield, South Yorkshire",
    "Crucible Theatre, 55 Norfolk Street, , , , Sheffield, South Yorkshire",
    "Sheffield Crucible Productions Ltd, 55 Norfolk Street, , , , Sheffield, South Yorkshire",
    "Sheffield Theatres Crucible Trust, 55 Norfolk Street, , , , Sheffield, South Yorkshire",
    "The City Of Sheffield Theatre Trust, 55 Norfolk Street, , , , Sheffield, South Yorkshire"
]
}

As you can see there's a lot of commas and spaces directly after with no data in.

How do I go about doing this? My current code is :

$.ajax(
            {
                type: 'POST',
                url: '/address',
                dataType: 'json',
                data: { _token: token, postcode: postcode},
                success: function( data )
                {
                    $('.addresses').html('');

                    $.each(data, function(k, v)
                    {
                        address = v.replace(/\s+/g, ' ');

                        $('.addresses').append('<option value="">'+ address +'</option>');
                    })
                }
            }
        )

Upvotes: 3

Views: 99

Answers (3)

Cameron Downer
Cameron Downer

Reputation: 2038

You could also do this without the use of regular expressions.

const address = "The City Of Sheffield Theatre Trust, 55 Norfolk Street, , , , Sheffield, South Yorkshire"

const cleanAddress = address.split(", ")
    .filter(section => !!section)
    .join(", ");

// The City Of Sheffield Theatre Trust, 55 Norfolk Street, Sheffield, South Yorkshire

This uses the fact that an empty string "" is a falsy value.

Adding this to your code:

$.ajax({
  type: "POST",
  url: "/address",
  dataType: "json",
  data: { _token: token, postcode: postcode },
  success: function(data) {
    $(".addresses").html("");

    $.each(data, function(k, v) {
     address = v.split(", ")
         .filter(section => !!section)
         .join(", ");

      $(".addresses").append('<option value="">' + address + "</option>");
    });
  }
});

Upvotes: 2

epascarello
epascarello

Reputation: 207511

You can use a regular expression to clean them up.

var str = "Crucible Enterprises Ltd, 55 Norfolk Street, , , , Sheffield, South Yorkshire"
var cleaned = str.replace(/,(\s,)+/, ',')
console.log(cleaned)

Upvotes: 2

Sohail Ashraf
Sohail Ashraf

Reputation: 10569

You could replace the extra , by using regex.

Try this.

let str = "Crucible Enterprises Ltd, 55 Norfolk Street, , , , Sheffield, South Yorkshire";
let regex = /(,\s){2,}/g
console.log(str.replace(regex, ", "));

In your code you could use like below.

$.ajax({
  type: "POST",
  url: "/address",
  dataType: "json",
  data: { _token: token, postcode: postcode },
  success: function(data) {
    $(".addresses").html("");

    $.each(data, function(k, v) {
      address = v.replace(/\s+|(,\s){2,}/g, " ");

      $(".addresses").append('<option value="">' + address + "</option>");
    });
  }
});

Upvotes: 1

Related Questions