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.

6
  • 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. Commented Apr 1, 2016 at 7: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.. Commented Apr 1, 2016 at 7: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. Commented Apr 1, 2016 at 7:46
  • @Mike'Pomax'Kamermans You are right... maybe should I remove double quotes? Commented Apr 1, 2016 at 7:50
  • Your code works fine. Please explicit not working properly. Commented Apr 1, 2016 at 7:59

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.