将变量作为字符串返回。

huangapple go评论50阅读模式
英文:

returning a variable in a span elements it is returning the variable as a string

问题

我有以下的代码,有没有办法在其中使用一个变量?它将变量返回为字符串而不是返回变量的值。

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

结果:

你能把这个放到library[amerMatch[i]]中吗?

英文:

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] + &quot;\\.&quot;, &quot;i&quot;), `&lt;span class=&quot;highlight&quot;&gt;library[amerMatch[i]]&lt;/span&gt;` + &quot;.&quot;)

result:

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

答案1

得分: 0

使用${}

`&lt;span class=&quot;highlight&quot;&gt;library[${amerMatch[i]}]&lt;/span&gt;`
英文:

use ${ }

`&lt;span class=&quot;highlight&quot;&gt;library[${amerMatch[i]}]&lt;/span&gt;`

</details>



# 答案2
**得分**: 0

也许你可以使用 `eval(replace)` 来获取库中 `amerMatch[i]` 的结果。
```javascript
replace = replace.replace(new RegExp(amerMatch[i] + "&quot;\\.&quot;", "i"), `<span class="highlight">library[amerMatch[i]]</span>` + ".")
var s = eval(replace)
英文:

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

replace = replace.replace(new RegExp(amerMatch[i] + &quot;\\.&quot;, &quot;i&quot;), `&lt;span class=&quot;highlight&quot;&gt;library[amerMatch[i]]&lt;/span&gt;` + &quot;.&quot;)
var s = eval(replace)

huangapple
  • 本文由 发表于 2023年6月19日 08:17:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502976.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定