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

213
Vistas
Better way to extract a string in Javascript

I have a string which is

xyz.com/test/classified/electronics/home-audio-turntables/amplifiers/new/.

I want to extract following value from above string i.e., /classified/electronics/home-audio-turntables/amplifiers/

I'm able to achieve this with the code below but is there a better way than this? Appreciate suggestions

window.location.href.split('test')[1].split('/new')[0]
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

A regular expression can match what comes between.

const str = 'xyz.com/test/classified/electronics/home-audio-turntables/amplifiers/new/';

const result = str.match(/test(\/.*?\/)new/)[1];
console.log(result);

Or with lookbehind

const str = 'xyz.com/test/classified/electronics/home-audio-turntables/amplifiers/new/';

const result = str.match(/(?<=test)\/.*?\/(?=new)/)[0];
console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

I think what you are looking for is window.location.pathname

In case of testing:

window.location.pathname.replace(/^(\/test)?/, '')
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