-2

Disclaimer: part of a homework for a course I am doing on javascript:

I am learning javascript regex and I don't know how to go about the following. Basically I have the following string:

  const a = 1;
  const e = 2;
  const b = '$a$/abc/$e$/'

Now I want to replace the $a$ and $e$ with a and e values so at least I have:

 const output = '1/abc/2'
Wede Asmera Tseada
  • 513
  • 2
  • 4
  • 14
  • Please see [how do I ask homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions). Without an honest effort on your part to complete it, the question is off-topic. – ggorlen Jul 26 '20 at 01:01
  • 2
    What did you try so far? In what way did it fail? If you plan how to do it in logical steps without knowing the exact functions and methods to use, what "tool" in your toolbox are you missing? – CherryDT Jul 26 '20 at 01:01
  • Partially a duplicate of [How do you use a variable in a regular expression?](https://stackoverflow.com/questions/494035/how-do-you-use-a-variable-in-a-regular-expression). Ideally, `a` and `e` wouldn’t be variables. An object would be a lot better here, e.g. `const lookup = { a: 1, e: 2 }`. Then, using the [`replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter) method you’d [look up the right `lookup` property](https://stackoverflow.com/a/4968448/4642212). – Sebastian Simon Jul 26 '20 at 01:05

1 Answers1

0
  • Use template literal bro for doing this :---
  const a = 1;
  const e = 2;
  const b = `${a}/abc/${e}/`
  
  console.log(b)


Anurag Kumar
  • 129
  • 1
  • 7