David G
David G

Reputation: 301

JavaScript regular expression, return everything before and after a pattern

I have the need to return a string (filename) without certain data, example strings are:

These example strings need to be returned as the following:

I've tried using the regex below, but i only see the pattern and after the pattern returned:

filename = fl.replace(/(^.*?(?=[_]{1}[0-9]{1,10}))/gi, '');

Thanks for your help.

Upvotes: 1

Views: 112

Answers (1)

anubhava
anubhava

Reputation: 785156

This regex should work:

var repl = str.replace(/_\d+(?=\.jpg$)/, "");

TESTING:

str = 'eng_somerset_yeovil_montacute-house_962.jpg';
var repl = str.replace(/_\d+(?=\.jpg$)/, "");
// eng_somerset_yeovil_montacute-house.jpg

Upvotes: 3

Related Questions