I get an address from a textbox with JQUERY, but it is a long string which is "Calle y No:XXXXXXXXXColonia:XXXXXXXXXMunicipio:XXXXXXXXXEstado:XXXXXXXXXCP:XXXXXX", from which I want to omit "Calle y No:","Colonia:", "Municipality:", "State:" and "CP:", I am applying the split function
var element = $("#txtDomicilio").val();
var fecha = element.split('Calle y No:');
var fecha1 = element.split('Colonia:');
var fecha2 = element.split('Municipio:');
var fecha3 = element.split('Estado:');
var fecha4 = element.split('CP:');
alert('Fecha formateada: ' + fecha);
alert('Fecha formateada: ' + fecha1);
alert('Fecha formateada: ' + fecha2);
alert('Fecha formateada: ' + fecha3);
alert('Fecha formateada: ' + fecha4);
but that way it only omits me for var fecha = element.split('Calle y No:');
Street and No:, but not Colonia:, Municipality: ..., How can I make it omit what I have in the split functions? all at once and not separately as I have it
If you know the names of the different parameters to be passed, you can add a special character in front of each of them, then break the string by that character you just created (using
split
), and what you are left with is an array with the parameter and its value separated by the:
original. You would only have to go through that new array and separate the values one by one (again usingsplit
).Something like this (I use an object to save it, but you could do any other operation):
Assuming the fields are always present and in that order, you can apply .split() iteratively in reverse, starting with the last one. And placing the obtained values in an array of results, pushing with .unshift() at the beginning of it.
Something like...