Search code examples
jqueryregexcountskip

Regex Jquery Skip/Count + 3 Characters


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!


Solution

  • Try match instead of split.

    var words = $(".Input_Field").val().match(/(\S.*?\..{3})/g)