关键字匹配并且高亮

... 2022-11-17 Less than 1 minute

# 关键字匹配并且高亮


function keywordscolorful(str, key) {
  const reg = new RegExp("(" + key + ")", "g");
  const newstr = str.replace(reg, "<font style='background:#ff0;'>$1</font>");
  return newstr;
}

const keysStr = keywordscolorful("123456", "2");

console.log(keysStr)

// "1<font style='background:#ff0;'>2</font>3456"

1
2
3
4
5
6
7
8
9
10
11
12
13
Last update: November 17, 2022 07:05
Contributors: Skr