javascript 正则表达式在同一行上有多个匹配项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10516458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex multiple matches on same line
提问by Phill Duffy
I have the following JavaScript Regex
我有以下 JavaScript 正则表达式
As used in http://regexpal.com/
\[.*(\.jpg|\.png|\.gif|\.bmp|\.jpeg).*\]
\[.*(\.jpg|\.png|\.gif|\.bmp|\.jpeg).*\]
As use in jQuery code -
在 jQuery 代码中使用 -
post.html().match(/\[.*(\.jpg|\.png|\.gif|\.bmp|\.jpeg).*\]/g);
post.html().match(/\[.*(\.jpg|\.png|\.gif|\.bmp|\.jpeg).*\]/g);
This is the sample data I am working with
这是我正在使用的示例数据
- [cid:[email protected]]
- s[cid:[email protected]]<
- image.jpg
- [cid:[email protected]]
- [cid:[email protected]]
- [cid:[email protected]]
- [[cid:[email protected]]
And again
[cid:[email protected]]]- test.gif
- [cid:[email protected]]
- s[cid:[email protected]]<
- 图片.jpg
- [cid:[email protected]]
- [cid:[email protected]]
- [cid:[email protected]]
- [[cid:[email protected]]
又是
[cid:[email protected]]]- 测试.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 行,我希望 [] 中包含的两个字符串分开,目前它将整行视为匹配,
回答by KARASZI István
You need to modify your regexp to change the greediness (note the .*?
):
你需要修改你的正则表达式来改变贪婪(注意.*?
):
\[.*?(\.jpg|\.png|\.gif|\.bmp|\.jpeg).*?\]