Bryan Lumbantobing
Bryan Lumbantobing

Reputation: 693

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

Upvotes: 1

Views: 1697

Answers (3)

The fourth bird
The fourth bird

Reputation: 163477

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

Upvotes: 2

DecPK
DecPK

Reputation: 25406

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 _

enter image description here

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

Upvotes: 1

Rickard Elimää
Rickard Elimää

Reputation: 7591

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

Upvotes: 1

Related Questions