1

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

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

blankData[i++](0) what the (0) means
@BryanLumbantobing The callback of replace takes a function. We can specify a function that returns a function where i is initialized with 0 and increments every time the function is called.
1

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

Comments

1

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

3 Comments

not understand the regex why is _+. why dont just ___ insted inside the global regex. don't know this increment index before btw thanks
ou 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 _
try to run your code with "hello ___________ where ___ test ___"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.