-2

Question:

I have this:

var questionExample0 = "Hi <name>, how are you doing? Well <name>, we have good news for you. <news>"

How do I change <name> and <news> to Stackoverflow and I joined Stackoverflow? (without using RegExp, Anchor to question using RegExp)

Community
  • 1
  • 1
Penguin
  • 93
  • 2
  • 10

1 Answers1

0

//String.prototype.replace with simple regex

var questionExample0 = "Hi <name>, how are you doing? Well <name>, we have good news for you. <news>";
questionExample0 = questionExample0.replace(/<name>/gi,'Stackoverflow').replace(/<news>/gi,'I joined Stackoverflow');
console.log(questionExample0);

//ES6 literal with variables

let name = 'Stackoverflow';
let news = 'I joined Stackoverflow';
var questionExample2 = `Hi ${name}, how are you doing? Well ${name}, we have good news for you. ${news}`;
console.log(questionExample2);