【发布时间】:2018-11-13 21:02:32
【问题描述】:
我正在努力实现:
const finalStr = "team='Core', team='Mechanics'"
//loop through string, get single quotes, add <bold>'Core'</bold>
//I want to return the string:
"team=<bold>'Core'</bold>, team=<bold>'Mechanics'</bold>"
我已经尝试过,但显然是错误的……无法理解:
const finalStr = this.state.finalString
const newFinal = finalStr.match(/'(.*?)'/g).map(item => {
item = item.replace(item, '<b>' + item + '</b>')
return item;
});
【问题讨论】:
-
无需循环,只需
replace一次:replace(/.../g, '<b>$&</b>') -
你想用它来生成 HTML 吗?如果是这样,正确的标签是
<b>,而不是<bold>,因为该标签不存在。 -
感谢@georg 完美!我像往常一样把它复杂化了。
标签: javascript regex dictionary match