简体   繁体   English

从javascript中的字符串数组替换字符串上的某些字符

[英]Replace certain character on string from an array of string in javascript

I have a string like this我有一个这样的字符串

const text = 'hello ___ where ___ test ___'

And also an array like this还有这样的数组

const blankData = ['there', 'you', 'it']

my expected result is我的预期结果是

hello there where you test it

what i have tried我试过的

const result = text?.replaceAll('___', (index, x) => {
    return blankData[index]
  })

I also getting with not ellegant idea like this我也有这样不优雅的想法

const mapped = text
    .split('___')
    .map((textitself, index) => textitself + blankData?.[index])

  mapped.pop()

 const result = mapped.join('')

is there better solution?有更好的解决方案吗?

im thinking of getting the index so i can replace for every index found but replace fould not getting index from it callback我正在考虑获取索引,这样我就可以为找到的每个索引进行替换,但替换不会从中获取索引回调

You could globally match ___ using the /g flag and get the index of the replacement from blankData be seeding a start value of 0 and increment it in every iteration.您可以使用/g标志全局匹配___并从blankData获取替换的索引,将blankData设为 0 并在每次迭代中增加它。

 const text = 'hello ___ where ___ test ___'; const blankData = ['there', 'you', 'it']; result = text.replace(/___/g, (i => _ => blankData[i++])(0)); console.log(result);

Note that if you don't want to have multiple matches for _________ but also don't want to match a single _ you can use _{3,} as the pattern to match 3 or more times an underscore.请注意,如果您不想对_________进行多个匹配,但也不想匹配单个_ ,则可以使用_{3,}作为模式来匹配 3 次或多次下划线。

 const text = 'h_ello ______ where ___ test ___'; const blankData = ['there', 'you', 'it']; result = text.replace(/_{3,}/g, (i => _ => blankData[i++])(0)); console.log(result);

You can use replace here, You can declare index outside of the replace or replaceAll method.您可以在这里使用replace ,您可以在replacereplaceAll方法之外声明index

You have hardcoded the replacement string ___ , What if there is one more _ so you can make it generic with using quantifiers _+ which is one or more _您已经对替换字符串___进行了硬编码,如果还有一个_怎么办,那么您可以使用one or more _量词_+使其通用

在此处输入图片说明

 const text = "hello ___ where ___ test ___"; const blankData = ["there", "you", "it"]; let index = 0; const result = text.replace(/_+/g, () => blankData[index++]); console.log(result);

You can also use replaceAll but you just have to take care of the index您也可以使用 replaceAll 但您只需要处理索引

 const text = "hello ___ where ___ test ___"; const blankData = ["there", "you", "it"]; let index = 0; const result = text?.replaceAll("___", () => { return blankData[index++]; }); console.log(result);

I already made a function for that.我已经为此做了一个功能。 I think the code is self-explanatory.我认为代码是不言自明的。

 function injectStrings(string, replacementsArr) { var pattern = /___/g, replacement = ''; return string.replace(pattern, function(match) { replacement = replacementsArr.shift(); if (typeof replacement == 'number' || typeof replacement == 'string') { return replacement; } // console.log('parameter for ' + match + ' is missing in \\"' + string + '\\"'); return ''; }); } const text = 'hello ___ where ___ test ___' const blankData = ['there', 'you', 'it'] // output: And also an array like this console.log( injectStrings(text, blankData) );

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

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