正则表达式在同一行上有多个匹配项

时间:2021-08-13 10:06:41

I have the following JavaScript Regex

我有以下JavaScript正则表达式

As used in http://regexpal.com/

用于http://regexpal.com/

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

As use in jQuery code -

在jQuery代码中使用 -

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

This is the sample data I am working with

这是我正在使用的示例数据

  1. [cid:image001.jpg@01CD2DC8.704399C0]
  2. s[cid:image001.png@01CD2DC8.704399C0]<
  3. image.jpg
  4. [cid:image002.jpg@01CD2DC8.704399C0]
  5. [cid:image002.exe@01CD2DC8.704399C0]
  6. [cid:image002.gif@01CD2DC8.704399C0]
  7. [[cid:image001.jpg@01CD2DE6.9802A2D0]
    And again
    [cid:image002.png@01CD2DE6.9802A2D0]]
  8. [[cid:image001.jpg@01CD2DE6.9802A2D0]又[cid:image002.png@01CD2DE6.9802A2D0]]

  9. 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,

我的问题是在第7行,我希望[]中包含的两个字符串是分开的,此时它将整行视为匹配,

1 个解决方案

#1


9  

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

你需要修改你的正则表达式来改变贪婪(注意。*?):

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

#1


9  

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

你需要修改你的正则表达式来改变贪婪(注意。*?):

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