简体   繁体   English

将数组后面所有出现的字符串替换为 javascript

[英]Replace all occurrences of string following an array with javascript

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]...我想用 images[0] 替换第一个 {image},用 images[1] 替换第二个 {image},用 images[2] 替换第三个 {image}...

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)

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.但是,对于更强大的方法,例如,如果您不只是替换 '{image}' 或者如果数组可能没有与字符串中的替换相同数量的参数等,您可能想要使用正则表达式。

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

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

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.请注意在字符串周围使用反引号而不是引号。

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这样做的好处是你只需要创建一个新的str副本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM