javascript - 查找链接图像并将其替换为随机字符串

标签 javascript

这是我的代码:

   var string="<div><ul><li><img src='http://example.com/img1.jpg' alt=''></li><li><img src='http://example.com/img2.jpg' alt=''></li><li><img src='http://example.com/img3.jpg' alt=''></li>   <li><img src='http://example.com/img4.jpg' alt=''></li><li><img src='http://example.com/img5.jpg' alt=''></li></ul></div>";

我想查找每个链接图像并将其替换为不重复的随机字符串。 示例:

var result= "<div><ul><li><img src='adssad' alt=''></li><li><img src='bsdafz' alt=''></li>  <li><img src='soweuosx' alt=''></li>    <li><img src='qlolzas' alt=''></li><li><img src='owueweuy' alt=''></li></ul></div>";

请帮助我!

最佳答案

试试这个。根据您的需要进行修改。

var string = "<div><ul><li><img src='http://example.com/img1.jpg' alt=''></li><li><img src='http://example.com/img2.jpg' alt=''></li><li><img src='http://example.com/img3.jpg' alt=''></li>   <li><img src='http://example.com/img4.jpg' alt=''></li><li><img src='http://example.com/img5.jpg' alt=''></li></ul></div>";

var output = string.replace(/'http:\/\/.*?'/g, function () {
  return "'" + (Math.random() * 16 | 1).toString(16) + "'"; // Return your own random string
});

alert(output);

关于javascript - 查找链接图像并将其替换为随机字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29768581/

相关文章:

javascript - DurandalJS 中启用模式的 app.css 在哪里?

javascript - 按钮显示进度

javascript - Twitter 分享按钮计数不正确

javascript - 图片不显示在 React App 中

javascript - 为什么 requirejs 试图将 '.js' 附加到使用 !text 插件加载的 .jst 模板文件?

javascript - 将变量与自身进行比较

javascript - 需要将 document.getElementsByTagName 替换为 document.getElementById

javascript - 使用 Ajax 返回加载的内容时功能不起作用

javascript - 使用 NodeJS 将开始和停止时间发布到 mysql(sequelize)

javascript - 在 json 中存储函数 [Javascript/Coffeescript]