简体   繁体   English

正则表达式在CKeditor的HTML中查找png-gif-jpg图像链接

[英]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.

相关问题 将 HTML Canvas 捕获为 gif/jpg/png/pdf? - Capture HTML Canvas as gif/jpg/png/pdf? 用于验证图像 url 的正则表达式,它可能包含 jpeg、jpg、gif、png 等扩展名 - Regex to validate image url which may contains extension like jpeg, jpg, gif, png 如何限制除jpg png或gif以外的图像大小和扩展名 - How to limit image size and extension other then jpg png or gif 如何找到所有.jpg,.png和.gif并使用JavaScript将它们列出到网格中? - How to find all .jpg, .png and .gif and list them into a grid with JavaScript? 在 png/jpg 图像中捕获 html div 的内容 - Capturing the content of a html div in a png/jpg image 如何将jpg或png图像放入HTML按钮中 - How to put a jpg or png image into a button in HTML 通过 angular 6 应用程序中的 javascript 更改 base64 图像大小并下载不同扩展名(png、jpg、gif 等) - Change base64 image size and download with different extension(png, jpg, gif, etc…) by javascript in angular 6 application 你如何将html div转换为图像,然后另存为gif或png? - How do you convert an html div to image and then save as gif or png? JS / Jquery,匹配找不到PNG =匹配(&#39;/ gif | jpg | jpeg | png /&#39;) - JS/Jquery, Match not finding the PNG = match('/gif|jpg|jpeg|png/') 将rel =“ lightbox”添加到其中包含.jpg和.gif的链接 - Add rel=“lightbox” to links with .jpg and .gif in them
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM