1

I wrote a simple JavaScript function to split a file name into parts: given a file name of the type 'image01.png' it splits it into 'image', '01', 'png'.

For this I use the following regular expression:

var reg = /(\D+)(\d+).(\S+)$/;

This works.

However, I would like to be able to split also something like this: day12Image01.png into 'day12Image', '01', 'png'. Generally, I would like to have any number of additional digits associated to the body as long as they do not fall right before the extension.

I tried with:

var reg = /(.+)(\d+).(\S+)$/;

or the alternative:

var reg = /(\S+)(\d+).(\S+)$/;

Confusingly (to me), if I apply those regular expressions to 'image01.png' I get following decomposition: 'image0', '1', 'png'.

Why is the '0' being assigned to the body instead of the numerical index in these cases?

Thanks for any feedback.

3 Answers 3

1

Try to use non-greedy regular expression /(\S+?)(\d+).(\S+)$/. As far as I know this should work for javascript.

Sign up to request clarification or add additional context in comments.

1 Comment

To be exact, this is greedy, or even more exact, lazy match.
0

Here is one possible regular expression that should work fine:

/^(.+?)(\d+)\.(\S+)$/

Note, you should escape a dot . character, since otherwise the regex will consider it as 'any character' (so called "Special dot").

1 Comment

Indeed it works. Thanks also for the heads up on the special dot.
0

By default, capture groups are greedy, they will capture as much as they can, and since + means one OR more, it can just match the last digit and leave the first to the . or the \S. Make them un-greedy with ?:

var reg = /(.+?)(\d+).(\S+)$/;

Or

var reg = /(\S+?)(\d+).(\S+)$/;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.