JavaScript正则表达式:在匹配的字符串中添加字符串(前后)

时间:2017-10-12 12:46:09

标签: javascript regex string replace

说我在JavaScript中有以下字符串:

var text = "This is Table1, and the next table is Table2"

我需要通过添加Table1来突出显示HTML中的Table2<b></b>粗体,例如<b>Table1</b>。使Table1 or 2之前/之后的单词变得更加困难。

text.replace(/Table\d+/g, "<b>Table</b>"); // how to reserve the number for Table1 or Table2 here?

由于

1 个答案:

答案 0 :(得分:0)

你可以做到

&#13;
&#13;
var text = "This is Table1, and the next table is Table2"

console.log(text.replace(/(Table\d+)/g, '<b>$1</b>'))
&#13;
&#13;
&#13;