0

I have the following code is there anyway to use a variable in it? It is returning the variable as a string and not returning the value of the variable.


 replace = replace.replace(new RegExp(amerMatch[i] + "\\.", "i"), `<span class="highlight">library[amerMatch[i]]</span>` + ".")

result:

Can you toss this in the library[amerMatch[i]] for me?

crmarc
  • 13
  • 4
  • I'm making an automatic question editing tool. Do you mind if I add this question to a dataset? – wizzwizz4 Jun 19 '23 at 00:22
  • Yeah feel free to use my question. Yeah that article helped I didn't know that it was called a template literal so it was hard to search for a solution. – crmarc Jun 19 '23 at 00:45

2 Answers2

0

use ${ }

`<span class="highlight">library[${amerMatch[i]}]</span>`
Khaled Alam
  • 885
  • 7
  • 12
0

maybe you can use eval(replace) to get the result of library[amerMatch[i]]

replace = replace.replace(new RegExp(amerMatch[i] + "\\.", "i"), `<span class="highlight">library[amerMatch[i]]</span>` + ".")
var s = eval(replace)
VinKang
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 20 '23 at 00:59