Javascript 函数从文本中获取图像URL

Javascript 函数从文本中获取图像URL,javascript,regex,image,url,Javascript,Regex,Image,Url,我正试图编写一个简单的javascript函数来从文本中提取img URL 我几乎让它工作,但我有一个问题与参数我如何才能删除它们,并返回完整的img网址,有人可以帮助不是最好的regexs document.write(getImagesInText("Whether the view should hidden from (i.e., ignored by) the accessibility service. http://jsfiddle.net/ http://thickbox.ne

我正试图编写一个简单的javascript函数来从文本中提取img URL

我几乎让它工作,但我有一个问题与参数我如何才能删除它们,并返回完整的img网址,有人可以帮助不是最好的regexs

document.write(getImagesInText("Whether the view should  hidden from (i.e., ignored by) the accessibility service. http://jsfiddle.net/ http://thickbox.net/images/plant4.jpg afhttp://thickbox.net/images/plant4.jpg?4t34t34t"));


function getImagesInText(text){

        var html = text;
        var urlRegex = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)+\.(?:jpg|jpeg|gif|png)/gi;
        return html.replace(urlRegex, '<img src="$1" width="48" height="48"/>');

}
document.write(getImagesInText)(“视图是否应该对辅助功能服务隐藏(即被忽略)”。http://jsfiddle.net/ http://thickbox.net/images/plant4.jpg afhttp://thickbox.net/images/plant4.jpg?4t34t34t"));
函数getImagesInText(文本){
var html=文本;
var urlRegex=/((ftp | http | https):\/\/(\w+:{0,1}\w*@)(\S+:[0-9]+)(\/\\/([\w |!:?+=&%@!-\/]))+。(?:jpg | jpeg | gif | png)/gi;
返回html.replace(urlRegex,”);
}
在此处查看示例。

更新

function getImagesInText( s ) {
    var html = s;
    var imgregex = /((http(s)?|ftp):\/\/[\S]*(\.jpg|.jpeg|\.gif|.png)[\S]*)/gi;
    var urlRegex = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;

    html = html.replace(/(\.jpg|\.jpeg|\.gif|\.png)/gi, function( ext ) { return ext + " "; }); 

    html = html.replace(/(http|https)/gi, function( ext ) { return " " + ext; });

    html = html.replace(urlRegex, function( path ) {
           if(path.match(/jpg|png|gif|jpeg/g)){
             return "<img width='48' height='48' src='" + path + "' />";
           }else{
             return "<a href='" + path + "'>" + path + "</a>";
           }
    });

    return html;
}
函数getImagesText(s){ var html=s; var imgregex=/((http(s)?ftp):\/\/[\s]*(\.jpg|.jpeg|\.gif|.png)[\s]*)/gi; var urlRegex=/((ftp | http | https):\/\/(\w+:{0,1}\w*@)(\S+:[0-9]+)(\/\\\/([\w!:?+=&%@!\-\/])/gi; html=html.replace(/(\.jpg\.jpeg\.gif\.png)/gi,函数(ext){return ext+“”;}); html=html.replace(/(http | https)/gi,函数(ext){return”“+ext;}); html=html.replace(urlRegex,函数(路径){ if(path.match(/jpg | png | gif | jpeg/g)){ 返回“”; }否则{ 返回“”; } }); 返回html; }

var s=“是否应该对辅助功能服务隐藏(即忽略)视图。http://jsfiddle.net/ http://thickbox.net/images/plant4.jpg afhttp://thickbox.net/images/plant4.jpg?4t34t34t";
console.log(getImagesInText))/“视图是否应该对辅助功能服务隐藏(即被忽略)。http://jsfiddle.net/  af“
函数GetImagesText(s){
var regex=/((http(s)?ftp):\/\/[\s]*(\.jpg\.jpeg\.gif\.png)[\s]*)/gi;
返回s.replace(正则表达式,函数(路径){
返回“”;
});
}
var s=“是否应该对辅助功能服务隐藏视图(即被忽略)。http://jsfiddle.net/ http://thickbox.net/images/plant4.jpg afhttp://thickbox.net/images/plant4.jpg?4t34t34t";
console.log(getImagesInText))/“视图是否应该对辅助功能服务隐藏(即被忽略)。http://jsfiddle.net/  af“
函数GetImagesText(s){
var regex=/((http(s)?ftp):\/\/[\s]*(\.jpg\.jpeg\.gif\.png)[\s]*)/gi;
返回s.replace(正则表达式,函数(路径){
返回“”;
});
}

hi null感谢这一点在我更新答案后我就是这样我为开始时插入的糟糕的替换函数提前道歉但我需要一种方法来处理用户是否直接将url插入文本中,这样它就不会中断thanks@user1503606我不明白你为什么要这么做?如果它只是一个url,那么它是有效的。顺便说一句,您可以将replace重写为:
s.replace(/(\.jpg\.jpeg\.gif\.png)/gi,function(ext){return ext+“”})
hi null如果您以@user1503606为例访问此链接,您可以看到我遇到的问题,我明白了。但是在这种情况下,我会将其视为一个url,而不是一张图片s.replace(/(\.jpg\.jpeg\.gif\.png)/gi,function(ext){return ext+“”})hi null如果您以@user1503606为例访问此链接,您可以看到我遇到的问题,我明白了。但是如果是那样的话,我会把它当作一个url,而不是一张图片var s = "Whether the view should hidden from (i.e., ignored by) the accessibility service. http://jsfiddle.net/ http://thickbox.net/images/plant4.jpg afhttp://thickbox.net/images/plant4.jpg?4t34t34t"; console.log( getImagesInText(s) ) // "Whether the view should hidden from (i.e., ignored by) the accessibility service. http://jsfiddle.net/ <img src='http://thickbox.net/images/plant4.jpg' /> af<img src='http://thickbox.net/images/plant4.jpg?4t34t34t' />" function getImagesInText( s ) { var regex = /((http(s)?|ftp):\/\/[\S]*(\.jpg|\.jpeg|\.gif|\.png)[\S]*)/gi; return s.replace(regex, function( path ) { return "<img src='" + path + "' />"; }); }