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

308
Vistas
Regex to filter two conditions: one after another one

Say I have string example_photo_name1.png and example_photo_name2.png. I want to remove everything after first . and everything before last _. The expected output is name1 and name2.

I could remove everything after first . by using pattern (.*)\.[^\.]*$. However, I do not know how to remove everything before the last _. How can I do this?

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

0

You could use pattern (?<=_)(?!.+_).+(?=\.)

Pattern explanation:

(?<=_) - positive lookbehind - assert what preceeds is underscore

(?!.+_) - negative lookahead - assert what follows does not contain any underscore (so we are sure we are just behind last underscore)

.+ - match one or more of any characters

(?=\.) - assert what follow is dot .

Regex demo

Matched text will be exactly what you want.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Something like this might work:

const string = 'example_photo_name1.png';
string.replace(/^.*_(.*?)\.[^.]*$/, '$1');

If you want the prefix xxx_ and the suffix .xxx to be optional then you can wrap them in non capturing groups and add the proper quantifier:

/^(?:.*_)?(.*?)(?:\.[^.]*)?$/

This way string like:

hello_world => world
world.jpg   => world
about 4 years ago · Juan Pablo Isaza Denunciar

0

The (.*)\.[^\.]*$ pattern of yours (used with .replace and $1) removes the last . and the rest of the string.

You can use

text = text.replace(/^.*_|\..*/g, '')

See the regex demo. Details:

  • ^.*_ - start of a string, any zero or more chars other than line break chars as many as possible, and then a _ char
  • | - or
  • \..* - a dot and then any zero or more chars other than line break chars as many as possible

See a JavaScript demo:

const texts = ['example_photo_name1.png', 'example_photo_name2.png'];
const re = /^.*_|\..*/g;
for (const text of texts) {
  console.log(text, '=>', text.replace(re, ''))
}

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