Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

298
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!