Home > Enterprise >  Replace all occurrences of string following an array with javascript
Replace all occurrences of string following an array with javascript

Time:04-02

I have a string like:

const content = 'Lorem ipsum dolor {image} sit amet, consectetur adipiscing elit {image}. Sed quis varius erat. Pellentesque in {image} magna feugiat mi imperdiet suscipit. Pellentesque eget lobortis justo. {image} Sed id pretium purus.'

And an array like:

const images = ['https://images.website.com/61ea8cc09233173e0ff27b1b.jpg','https://images.website.com/61ea8cc39233173e0ff27b24.jpg','https://images.website.com/61ea8cc59233173e0ff27b2d.jpg','https://images.website.com/61ea8cc89233173e0ff27b36.jpg']

And I would like to replace first {image} with images[0], second {image} with images[1], , third {image} with images[2]...

CodePudding user response:

This script can be used for this purpose

let s="{image} b {image} c {image}"
let images=['image1','image2','image3']
images.forEach(img=>{s=s.replace('{image}',img)})
console.log(s)

CodePudding user response:

It really depends on what you are trying to do. The simplest approach would be the following. For a more robust approach though, for instance, if you aren't just replacing '{image}' or if the array possibly doesn't have the same number of parameters as the replacements in the strings, etc, you will probably want to use RegEx.

var content = 'Lorem ipsum dolor {image} sit amet, consectetur adipiscing elit {image}. Sed quis varius erat. Pellentesque in {image} magna feugiat mi imperdiet suscipit. Pellentesque eget lobortis justo. {image} Sed id pretium purus.';
const images = ['https://images.website.com/61ea8cc09233173e0ff27b1b.jpg','https://images.website.com/61ea8cc39233173e0ff27b24.jpg','https://images.website.com/61ea8cc59233173e0ff27b2d.jpg','https://images.website.com/61ea8cc89233173e0ff27b36.jpg'];

for (var i = 0, l = images.length; i < l; i  ) {
  content = content.replace('{image}', images[i]);
}

console.log(content);

CodePudding user response:

You can do as follow:

const content = 'Lorem ipsum dolor {image} sit amet, consectetur adipiscing elit {image}. Sed quis varius erat. Pellentesque in {image} magna feugiat mi imperdiet suscipit. Pellentesque eget lobortis justo. {image} Sed id pretium purus.'

const images = ['https://images.website.com/61ea8cc09233173e0ff27b1b.jpg','https://images.website.com/61ea8cc39233173e0ff27b24.jpg','https://images.website.com/61ea8cc59233173e0ff27b2d.jpg','https://images.website.com/61ea8cc89233173e0ff27b36.jpg']

let result = content
images.forEach((img) => {
    result = result.replace('{image}', img)
})
console.log(result);

CodePudding user response:

Use a template literal:

const content = `Lorem ipsum dolor ${image[0]} sit amet, consectetur adipiscing elit ${image[1]}. Sed quis varius erat. Pellentesque in ${image[2]} magna feugiat mi imperdiet suscipit. Pellentesque eget lobortis justo. ${image[3]} Sed id pretium purus.`

Note the use of backticks around the string instead of quotes.

CodePudding user response:

function replaceWithImages(str, images) {
  const images_ = [...images].reverse();
  return str.replace(/{image}/g, () => images_.pop() || '');
}

The benefit of doing it this way is that you only need to create one new copy of str

  • Related