Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

83
Vistas
javascript split with line or space

I'm trying to split this code

<h1>
                                   999 street
                                   <br>
                                   city
                                   , 
                                   AA
                                   
                                   90777
                                   <br>
      </h1>

So far I can get this:

const address = document.querySelector('h1')

let street = address.split(',')
let pre_postalCode = street[2]

So I can get the part after , But the following code doesn't work

let pre_postalCode2 = pre_postalCode.split(' ')
let postalCode = pre_postalCode2[1]

I can't get the "90777", it returns nothing I tried something like

let pre_postalCode2 = pre_postalCode.replace(/\br/g, " ").split(" ")

It doesn't work

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

With this regex, you can split the text by newline value. After this, you must have a pattern or you must know the order of the specific word/line that you want to get the value from. For example, if you know that the postal code will always be on the 8th line then you can do this.

const data = `<h1>
                                   999 street
                                   <br>
                                   city
                                   , 
                                   AA
                                   
                                   90777
                                   <br>
      </h1>`;
let values = data.split(/\r?\n/).map(n => n.trim());
console.log(values[7]);

about 4 years ago · Juan Pablo Isaza Denunciar

0

I hope this helps.

var arr = `<h1>
                                   999 street
                                   <br>
                                   city
                                   , 
                                   AA
                                   
                                   90777
                                   <br>
      </h1>`.split("\n").map(val => val.trim()).filter(val =>  val && val !== "<br>" && val !== ",");
console.log(arr);
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can identify just numbers from some parts of the string and work with them:

const str = `<h1>
                                   999 street
                                   <br>
                                   city
                                   , 
                                   AA
                                   
                                   90777
                                   <br>
      </h1>`

matches = str.split(',')[1].match(/\d+/g);

matches.forEach(res => console.log(res));

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda