skip to Main Content

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?

2

Answers


  1. use ${ }

    `<span class="highlight">library[${amerMatch[i]}]</span>`
    
    Login or Signup to reply.
  2. 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)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search