javascript - jQuery 仅更改字符串的一部分

标签 javascript jquery

我在输入文件中有这个字符串。

<input type="file" data-url="/modules/wizard/upload.php?eid=18000115&amp;type=ico&amp;case=protagonist&amp;id=121001118" value="" class="wizard_image" name="files">


data-url="/modules/wizard/upload.php?eid=18000115&amp;type=ico&amp;case=protagonist&amp;id=121001118"

现在,我只想用不同的内容更改该字符串的最后一个参数:id=121001118,而不是 data-url 属性的整个值。

我该怎么做?下面的字符串将更改整个字符串,这不是我想要的。

newBox.find('input.wizard_image').attr('data-url', 'somethingElse');

感谢您的帮助

最佳答案

您可以使用 regular expression :

newBox.find('input.wizard_image').attr('data-url', function(i, val) {
    return val.replace(/id=\d+$/, 'id=somethingElse');
});

Passing a function to .attr可以轻松修改现有值。

表达式解释:

id= // literally matches "id="
\d+ // matches one or more digits 
$   // matches the end of the line/string

关于javascript - jQuery 仅更改字符串的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17405602/

相关文章:

php - 如何从 php 发送 json 到 highcharts

javascript src 无法从父级 jquery 加载中工作

javascript - 在 Android Studio 中使用 JavaScript

javascript - 如何通过在 JavaScript 中调用函数来使用 onclick 更改 div 的颜色?

javascript - onclick 中的字符串

javascript - 闭合方式有什么区别

javascript - 带有标签的 Twitter Bootstrap 下拉列表

jquery - 如何让 JQuery 验证在空输入上调用远程规则

jquery - 不裁剪所选区域

javascript - jQuery 完整版和缩小版主要功能差异?