[英]Regex to find png-gif-jpg image links in HTML of CKeditor
I am using the following regex to intercept posted image links in CKeditor: 我使用以下正则表达式拦截CKeditor中的已发布图像链接:
var editorContent = String(qa_ckeditor_content.getData());
if( editorContent.match(/(https?:\/\/\S+\.(?:jpg|png|gif|jpg<br|png<br|gif<br|jpg<\/p>|png<\/p>|gif<\/p>))\s+/) != null ) {
alert('Sorry, image links not allowed.');
return false;
}
However, this will not find something like: 但是,这不会发现:
<a href="#">myimage.png</a>
I am trying to find the regex the looks for png<...
(plus following characters). 我试图找到正则表达式寻找
png<...
(加上下面的字符)。 I tried that using a dot which seems not correct: 我尝试使用一个似乎不正确的点:
/(https?:\/\/\S+\.(?:jpg|png|gif|jpg<.|png<.|gif<.))\s+/
I know that this is a beginner question, but I failed finding the right solution :-( 我知道这是一个初学者的问题,但我找不到合适的解决方案:-(
Thank you for your time! 感谢您的时间!
First of all, I would like to point out the false security of blacklisting. 首先,我想指出黑名单的虚假安全性。 There will always be that other case you didn't think of that makes it through.
总会有你没想到的其他情况通过。
That being said; 话虽如此; you could just have the the regex search for .jpg, .png, .gif etc. followed by anything other than a word character.
你可以让正则表达式搜索.jpg,.png,.gif等,然后搜索除了单词字符以外的任何内容。
/\.(jpg|png|gif)\b/
This will match those extensions in any case I can think of at least, and can replace that entire regex you have so far. 在任何我能想到的情况下,这将匹配那些扩展,并且可以替换到目前为止你所拥有的整个正则表达式。
Try this regex: 试试这个正则表达式:
<a[^>]+>(.+?\.(?:jpg|png|gif))<
and a sample code: 和示例代码:
match = inputString.match(/<a[^>]+>(.+?\.(?:jpg|png|gif))</);
if (match != null) {
// matched text: match[0]
file = match[1];
} else {
// Match attempt failed
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.