简体   繁体   English

在字符串中查找id并从数组中替换

[英]Find ids in string and replace from array

I have a string variable which could be something like: 我有一个字符串变量,可能是这样的:

var content = "blah blah [media:id=1] blah blah blah [media:id=0] blah blah";

And an array of Objects with Image information ie 和一组带有图像信息的对象即

var imageArray = [
  {id:0, path:"/images/image.jpg"},
  {id:1, path:"/images/anotherimage.jpg"}
]

I need a function to replace the "[media:id=x]" with the corresponding image object path from the imageArray via the id, so that my 'content' variable looks like: 我需要一个函数来替换“[media:id = x]”和imageArray中相应的图像对象路径通过id,这样我的'content'变量看起来像:

"blah blah <img src='/images/anotherimage.jpg' /> blah blah blah <img src='/images/image.jpg' /> blah blah";

But I'm not sure where to start with this? 但我不知道从哪里开始呢?

You can use String#replace to replace the id s in the string by it's respective image element. 您可以使用String#replace通过它的相应图像元素替换字符串中的id

content.replace(/\[media:id=(\d+)\]/g, function(m, id) {
    var path = imageArray.find(o => o.id === Number(id));
    if (path) {
        return `<img src="${path.path}" />`;
    }
    return m;
});

The regex \\[media:id=(\\d+)\\] will match 正则表达式\\[media:id=(\\d+)\\]将匹配

  1. \\[ : [ literal \\[[字面
  2. media:id= literal media:id= literal
  3. (\\d+) : One or more number and put it into captured group (\\d+) :一个或多个数字并将其放入捕获的组中
  4. \\] : ] literal \\]]字面意思

Array#find can be used to find particular element in the array. Array#find可用于查找数组中的特定元素。

imageArray.find(o => o.id === Number(id))

will get the object whose id matches the matched id from the string. 将从字符串中获取id与匹配的id匹配的对象。 If object is found in the array, the img tag can be returned to replace it with the element else, complete string can be returned. 如果在数组中找到object,则可以返回img标记以将其替换为else else,可以返回完整的字符串。

 var content = "blah blah [media:id=1] blah blah blah [media:id=0] blah blah"; var imageArray = [{ id: 0, path: "/images/image.jpg" }, { id: 1, path: "/images/anotherimage.jpg" }]; var res = content.replace(/\\[media:id=(\\d+)\\]/g, function(m, id) { var path = imageArray.find(o => o.id === Number(id)); if (path) { return `<img src="${path.path}" />`; } return m; }); console.log(res); 

I'd do it like this: 我这样做:
I go through the array and check for each id if it exists in the string, if so I replace that part. 我遍历数组并检查每个id是否存在于字符串中,如果是,我替换该部分。

 var content = "blah blah [media:id=1] blah blah blah [media:id=0] blah blah"; var imageArray = [ {id:0, path:"/images/image.jpg"}, {id:1, path:"/images/anotherimage.jpg"} ] imageArray.forEach(function(element, i){ var searchedStr= "[media:id="+element.id+"]"; if(content.indexOf(searchedStr)!==-1) { var replaceStr = "<img src='"+element.path+"' />"; content = content.replace(searchedStr,replaceStr); } }); console.log(content); 

A simple array reduce could help you: 简单的数组缩减可以帮助您:

 var content = "blah blah [media:id=1] blah blah blah [media:id=0] blah blah"; var imageArray = [ {id:0, path:"/images/image.jpg"}, {id:1, path:"/images/anotherimage.jpg"} ] console.log( imageArray.reduce((res, obj) => { const {id, path} = obj, re = new RegExp(`\\\\[media:id=${id}\\\\]`) ; return res.replace(re, `<img src="${path}" />`); }, content) ); 

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

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