替换html内容中的多个字符串

时间:2013-07-22 09:16:10

标签: javascript jquery string replace

我想使用jquery用一个字符串替换所有“replace”字符串,同时保留所有扩展名(.jpg,.mp4,...)。这是我的尝试:

<video id="test" controls="controls" poster="replace.jpg" width="640" height="360">
    <source src="replace.mp4" type="video/mp4" />
    <source src="replace.webm" type="video/webm" />
    <source src="replace.ogg" type="video/ogg" />
    <object type="application/x-shockwave-flash"     data="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" width="640" height="360">
        <param name="movie" value="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" />
        <param name="allowFullScreen" value="true" />
        <param name="wmode" value="transparent" />
        <param name="flashVars" value="config={'playlist':['replace.jpg',{'url':'replace.mp4','autoPlay':false}]}" />
        <img alt="Test Video" src="replace.jpg" width="640" height="360" title="No video playback capabilities, please use a better browser." />
    </object>
    </video>

和脚本:

var src = "content/test";
$("#test").replace(/replace/g , src);

2 个答案:

答案 0 :(得分:0)

你需要这个 -

$("#test").html(function(_,htm){
  return htm.replace(/replace/g , src);
});

答案 1 :(得分:0)

请改用:

DEMO

$("#test").replaceWith(function(){
  return this.outerHTML.replace(/replace/g , src);
});