I've been looking for an efficient way to do this but haven't been able to find it, basically what I need is that given this url for example:

http://localhost/mysite/includes/phpThumb.php?src=http://media2.jupix.co.uk/v3/clients/4/properties/795/IMG_795_1_large.jpg&w=592&aoe=1&q=100

I'd like to be able to change the URL in the src parameter with another value using javascript or jquery, is this possible?

提前谢谢.

推荐答案

Wouldn't this be a better solution?

var text = 'http://localhost/mysite/includes/phpThumb.php?src=http://media2.jupix.co.uk/v3/clients/4/properties/795/IMG_795_1_large.jpg&w=592&aoe=1&q=100';
var newSrc = 'www.google.com';
var newText = text.replace(/(src=).*?(&)/,'$1' + newSrc + '$2');

编辑:

added some clarity in code and kept 'src' in the resulting link

$1表示()(即)src=中的第一部分,$2表示()(即)&中的第二部分,因此这表示您将在src&之间更改该值.更清楚的是,应该是这样的:

src='changed value'& // this is to be replaced with your original url

用于替换所有眼病的附加组件:

If you have several parameters with the same name, you can append to the regex global flag, like this text.replace(/(src=).*?(&)/g,'$1' + newSrc + '$2'); and that will replaces all the values for those params that shares the same name.

Jquery相关问答推荐

多个 AJAX 调用;获取所有失败的呼叫

通过 jQuery 提取 application/json 数据

如何使 jQuery.ready 中定义的函数在全局可用?

加载外部 css 文件,如 jquery 中的脚本,这在 ie 中也兼容

jQuery 时间 Select 器

在 jQuery 中构建 html 元素的最清晰方法

如何在 bootstrap 模式关闭并重新打开时重置 bootstrap 模式?

如何使用Angular 检测浏览器后退按钮单击事件?

Ajax 更新后在 jQuery 中重新绑定事件(更新面板)

javascript中的描述关键字

未捕获的语法错误:无法在文档上执行querySelector

jQuery计算所有文本字段中的值的总和

使用jquery设置di​​v标签的值

使用 jQuery 获取选定的选项 id

如何检测 window.print() 完成

如何使用 JQuery 创建一个新的 img 标签,其中包含来自 JavaScript 对象的 src 和 id?

如何在新窗口中打开链接?

无法更新数据属性值

将变量的值复制到另一个

如何在 jquery 中获取 textarea 的值?