Separating text with Javascript

Asked

Viewed 720 times

2

I have a string and need to separate the name of the street, neighborhood, city and state.

string:

Rua tal Muniz, 124 - ramones, Sapucaia - SP

How can I do it using Javascript? (Can be a solution with or without using jQuery library)

  • The address always comes in this format there or may have variations?

  • always comes in this format

  • Could you put exactly how your array is, along with the code you tried to do? In what information was difficult?

  • @Andersoncarloswoss the information comes so in plain text I would need the separate information

  • .split(",") and subsequent split(" - ") may solve your problem. Only you can say

  • I haven’t done the code yet, I need a north to follow I have no idea how to start I need tips only

  • And the array you quoted, where it is?

  • You can start by trying to split the string into commas, then split the hyphen.

  • 2

    It is important to remember that in this idea if any of the information already has a comma or hyphen creates a problem.

  • 2

    @Josimara Just a brief explanation of my edition. In case you commented that the information came in text then I believe that you do not actually have an array but a string. Another point is that you put "javascript or jquery" this "or" can allow a misinterpretation to the reader, which is the idea that jquery is not javascript, and, it is nothing more than a set of functions in javascript. I hope I’ve helped :)

  • Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).

Show 6 more comments

3 answers

1

One way is using the position of the separators as below:

const separar = endereco => {
  let posicao = endereco.indexOf(',');
  const rua = endereco.substring(0, posicao).trim();
  let aux = endereco.substring(posicao + 1).trim();
  posicao = aux.indexOf('-');
  const numero = aux.substring(0, posicao).trim();
  aux = aux.substring(posicao + 1).trim();
  posicao = aux.indexOf(',');
  const bairro = aux.substring(0, posicao);
  aux = aux.substring(posicao + 1).trim();
  posicao = aux.indexOf('-');
  const cidade = aux.substring(0, posicao).trim();
  aux = aux.substring(posicao + 1).trim();
  posicao = aux.indexOf('-');
  const estado = aux;
  
  return {
    rua,
    numero,
    bairro,
    cidade,
    estado,
  }
}

console.log(separar('Rua tal Muniz, 124 - ramones, Sapucaia - SP'));

Another way is by using the following regular expression:

/^(.+),(.+)-(.+),(.+)-(.+)$/gm

As follows:

const ENDERECO = /^(.+),(.+)-(.+),(.+)-(.+)$/gm;

const separar = endereco => {
  const match = ENDERECO.exec(endereco);

  return {
    rua: match[1],
    numero: match[2],
    bairro: match[3],
    cidade: match[4],
    estado: match[5],
  }
}

console.log(separar('Rua tal Muniz, 124 - ramones, Sapucaia - SP'));

  • ^ ensures the position is at the beginning of the line
  • Captures the group (.+):
    • .+ combines any character (except line terminators);
    • The quantifier + combines between 1 or unlimited times, as many times as possible;
  • , combines the literal character ,;
  • Captures the group (.+):
    • .+ combines any character (except line terminators);
    • The quantifier + combines between 1 or unlimited times, as many times as possible;
  • - combines the literal character -;
  • Captures the group (.+):
    • .+ combines any character (except line terminators);
    • The quantifier + combines between 1 or unlimited times, as many times as possible;
  • , combines the literal character ,;
  • Captures the group (.+):
    • .+ combines any character (except line terminators);
    • The quantifier + combines between 1 or unlimited times, as many times as possible;
  • - combines the literal character -;
  • Captures the group (.+);
  • $ ensures the position is at the end of the line.

0

If the string always follow this pattern, you can try something like this:

var endereco = 'Rua tal Muniz, 124 - ramones, Sapucaia - SP';
var separado = endereco.split(',');

var rua     = separado[0].trim();
var bairro  = separado[1].split('-')[1].trim();
var cidade  = separado[2].split('-')[0].trim();
var estado  = separado[2].split('-')[1].trim();

console.log('Rua: ' + rua);
console.log('Bairro: ' + bairro);
console.log('Cidade: '+ cidade);
console.log('Estado: '+ estado);

0

Try using the code below. Testing here, it works. Before breaking the text by "," make a replacement of the "-" by a comma.

function myFunction() {
    var str = "Rua tal Muniz, 124 - ramones, Sapucaia - SP";
    str = str.replace("-", ",");
    var res = str.split(",");
    //variável res agora é um array que você pode percorrer com um loop ou posições fixas
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.