0

String has already been defined as follows:

const desc = `This is the password you use to access the $0 application. It is not the password for the $1 card. \n With the password, we can only read your statement. No other operations can be performed.`;

Array has already been defined as follows:

const params = ['American', 'American Black'];

I would like to replace:

$0 => American,
$1 => American Black

Expected outcome:

This is the password you use to access the American application. It is not the password for the American Black card. \n With the password, we can only read your statement. No other operations can be performed.

2
  • right, and yours? Commented Mar 23, 2021 at 22:12
  • 1
    can you change the template string? Commented Mar 23, 2021 at 22:13

4 Answers 4

1

If you want to make it work for N parameters, you can do:

const desc = `This is the password you use to access the $0 application. It is not the password for the $1 card. \n With the password, we can only read your statement. No other operations can be performed. Added $2 value, Added $3 value`;
const params = ['American', 'American Black', 'test1', 'test2'];

const result = params.reduce((acum, val, index) => acum.replace("$"+index, val), desc);

console.log(result);

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

1 Comment

thank you very much! I just made a small change in the regex to replace all occurrences. This "$"+index to new RegExp(\\$${index}, 'g')
0

Try this:

const params = ['American', 'American Black'];

const desc = `This is the password you use to access the ${params[0]} application. It is not the password for the ${params[1]} card.\nWith the password, we can only read your statement. No other operations can be performed.`;

console.log(desc)

Always be extra cautious when dealing with credit cards and money. Storing such data as a variable on the user-side code is quite dangerous, as it can be read by anyone quite easily.

Comments

0

If you can't edit the string desc then you can replace $0 and $1 as follows:

x = desc.split('$0').join(params[0])
x = x.split('$1').join(params[1])

return x

Comments

0

if you don't want to change the two constants you could simply replace it with:

var newDesc = desc.replace("$0", params[0]).replace("$1", params[1]);

Comments

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.