Regex Jquery Skip/Count + 3 Characters

Go To StackoverFlow.com

0

Given a string such as: "2.jpg 3 Pic.png 4 Pic For.gif ..." I need to get each image name. Ideally, with regex, I'd start at the period, then count 3 characters, and voila.

What I've got so far trims the file type too, I want to preserve that, since it varies.

    var words = $(".Input_Field").val().split(/\..{3} /g);

The above produces:

"2,3 Pic,4 Pic For, ..."

I want the extension back;

"2.jpg,3 Pic.png,4 Pic For.gif, ..."

EDIT

Just to clarify, the issue is that the filename has spaces, and there's a space after the extension. What I have going for me is that extensions will always be .xxx .

Any tips?

SOLUTION

(/(.*?..{3}) /g)

Produces empties almost every other array item (not quite), easy to get rid of with an if clause.

Thanks everyone!

2012-04-05 21:00
by C_K
Are you looking to get the whole image name or just the extension? I'm confused because you say "I'd start at the period, then count 3 characters, and voila" which implies you only need the extension - j-u-s-t-i-n 2012-04-05 21:17


3

Try match instead of split.

var words = $(".Input_Field").val().match(/(\S.*?\..{3})/g)
2012-04-05 21:11
by robkub
This works but you'll have a trailing whitespace on your filenames.. So remember to trim them. - barsju 2012-04-05 21:15
Changed the Regex, should now be without the trailing whitespac - robkub 2012-04-05 21:21
@robkub - I went with (/(.*?..{3}) /g), which produced empties that were easy to get rid of with an if clause. Together, works perfect. Thanks - C_K 2012-04-05 21:42
Ads