We are required to write a JavaScript function that takes in string value as the first argument and a pattern string as the second argument.
Suppose the string and the pattern are −
const str = '123456789'; const pattern = '## ## ## ###';
Then the function should pad the string according to the pattern and the output string should be −
const output = '12 34 56 789';
Example
const str = '123456789';
const pattern = '## ## ## ###';
const maskString = (str, pattern) => {
let i = 0;
const padded = pattern.replace(/#/g, () => {
return str[i++];
});
return padded;
};
console.log(maskString(str, pattern));Output
And the output in the console will be −
12 34 56 789