How can I replace multiple string at once? for example, there is code like below :
// the 'content' is part of html, so it all addjoined,
// but I delete some tags and sepeate tag to look clean
// *** the content is mongodb data
var content ='<p>ggfhjghgk<a class="mg-popup-img" href="/uploads/2016040117124079.png"><img class="pop" style="max-width: 100%; max-height: 100%;" src="/uploads/2016040117124079.png" /></a></p>
'
What I am going to do is, replace image name to new one. So every file name will be different. like this :
<p>ggfhjghgk<a class="mg-popup-img" href="/uploads/2016050117124079.png"><img class="pop" style="max-width: 100%; max-height: 100%;" src="/uploads/2016050117124079.png" /></a></p>
I googled, and found several docuemnts. like this : Replace multiple strings at once So I tried to
String.prototype.replaceArray = function(find, replace) {
var replaceString = this;
for (var i = 0; i < find.length; i++) {
replaceString = replaceString.replace(find[i], replace[i]);
}
return replaceString;
};
var originals = ['20160401', '20160402', '20160403'];
var newnames = ['20160501', '20160502', '20160503'];
var newcontent = content.replaceArray(originals, newnames);
console.log(newcontent); // not working properly.-> It's not changed.
How can I do this propery? Actually, I'm trying to implement 'Article Copy Task'. When user copy article, if the article have image tags I should copy image file and replace image src to new one. Appreciated any pointers... Thanks.