0

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.

Community
  • 1
  • 1
ton1
  • 7,238
  • 18
  • 71
  • 126
  • please clean up your code, or explain it better. As it stands, the code you're showing is **not** real JavaScript. Are you using React JSX? Mention that. But even when you do, that `content` assignment will absolutely not work. – Mike 'Pomax' Kamermans Apr 01 '16 at 07:42
  • @Mike'Pomax'Kamermans The `content` is actullay mongo db data, it has many double quotes. Sorry but I don't know how can I express it.. – ton1 Apr 01 '16 at 07:44
  • so explain that in your question. You're getting data from mongodb. Imaging you had to write a unit test: what would content *actually* contain for it to keep working without the mongodb connection? Show *that*. – Mike 'Pomax' Kamermans Apr 01 '16 at 07:46
  • @Mike'Pomax'Kamermans You are right... maybe should I remove double quotes? – ton1 Apr 01 '16 at 07:50
  • Your code works fine. Please explicit *not working properly*. – Shanoor Apr 01 '16 at 07:59
  • It's my fault... This question should be closed. – ton1 Apr 01 '16 at 08:59

1 Answers1

2

If it's one single string you could regex it:

// search for /uploads/(anything).png
var regex = /(\/uploads\/)(.+?)(\.png)/gi;
var counter = 0;
var str = '<img class="pop" src="/uploads/20160401.png" /><img class="pop" src="/uploads/20160402.png" /><img class="pop" src="/uploads/20160403.png" />';

// replace /uploads/(anything).png with /uploads/mynewfile(counter).png
var result = str.replace(regex, function () {
  var path = arguments[1],
      extension = arguments[3];

  return path + 'mynewfile' + (++counter) + extension;
});

console.log(str);
console.log(result);
Dieterg
  • 16,118
  • 3
  • 30
  • 49