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

156
Vistas
NodeJS - extract only the first part of an absolute path using regex (closed)

Say we have a file path like the following...

This/is/my/path/to/file.txt

How would I get the first part of this file path and then get rid of the rest of the path, so I'm left with an output like below?

This/

I've managed to construct the following regex below...

var file = "this/is/my/path/to/file.txt"

var regex = file.match(/\/(.+?)\//)[1]
var str = file;
var output = str.match(regex);

console.log(output);

but it's not doing what I want, it's only returning the second part of the file path wrapped in square brackets as shown below...

[
 "is"
]

I am very new to Regex in JavaScript so I'm not sure where I'm going wrong, I've tried on this for about 4 hours, can anyone please help?

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

0

you are doing one match and with the result you are doing a second match, I don't really understand, but in my solution there is just a regex to match anything except a slash plus one slash (that gives This/, just like in your example)

var file = "This/is/my/path/to/file.txt";

var regex = /[^/]+\//;
var [output] = file.match(regex);

console.log(output);

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