最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex multiple matches on same line - Stack Overflow

programmeradmin2浏览0评论

I have the following JavaScript Regex

As used in /

\[.*(\.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,

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,

Share Improve this question asked May 9, 2012 at 12:47 Phill DuffyPhill Duffy 2,8663 gold badges33 silver badges48 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 20

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

\[.*?(\.jpg|\.png|\.gif|\.bmp|\.jpeg).*?\]
发布评论

评论列表(0)

  1. 暂无评论