简体   繁体   中英

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.

 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.

 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.

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 _

在此处输入图片说明

 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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