Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex multiple matches on same line

I have the following JavaScript Regex

As used in http://regexpal.com/

\[.*(\.jpg|\.png|\.gif|\.bmp|\.jpeg).*\]

As use in jQuery code -

post.html().match(/\[.*(\.jpg|\.png|\.gif|\.bmp|\.jpeg).*\]/g);

This is the sample data I am working with

  1. [cid:[email protected]]
  2. s[cid:[email protected]]<
  3. image.jpg
  4. [cid:[email protected]]
  5. [cid:[email protected]]
  6. [cid:[email protected]]
  7. [[cid:[email protected]]
    And again
    [cid:[email protected]]]
  8. test.gif

My issue is that on line 7, I would like the two strings enclosed in the [] to be separate, at the moment it is treating the whole line as a match,

like image 342
Phill Duffy Avatar asked May 09 '12 12:05

Phill Duffy


1 Answers

You need to modify your regexp to change the greediness (note the .*?):

\[.*?(\.jpg|\.png|\.gif|\.bmp|\.jpeg).*?\]
like image 109
KARASZI István Avatar answered Sep 22 '22 14:09

KARASZI István