Separate text with Javascript [closed]

2

I have a string and need to separate the name from 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 the use of the jQuery library)

    
asked by anonymous 05.10.2018 / 22:39

3 answers

1

One way is to use the position of the tabs 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 to use 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 position is at the beginning of the line
  • Capture group (.+) :
    • .+ matches any character (except line terminators);
    • The + quantifier combines between 1 or unlimited times, as many times as possible;
  • , matches the literal character , ;
  • Capture group (.+) :
    • .+ matches any character (except line terminators);
    • The + quantifier combines between 1 or unlimited times, as many times as possible;
  • - matches the literal character - ;
  • Capture group (.+) :
    • .+ matches any character (except line terminators);
    • The + quantifier combines between 1 or unlimited times, as many times as possible;
  • , matches the literal character , ;
  • Capture group (.+) :
    • .+ matches any character (except line terminators);
    • The + quantifier combines between 1 or unlimited times, as many times as possible;
  • - matches the literal character - ;
  • Capture the group (.+) ;
  • $ ensures that the position is at the end of the line.
06.10.2018 / 00:07
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);
    
05.10.2018 / 22:53
0

Try using the code below. Testing here, it works. Before you can break the text by "," make a reset of the "-" with 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
}
    
05.10.2018 / 22:57