0

什么是用于在给定字符串中搜索大量名称的优秀 JavaScript 库。

例如,给定一个包含 1000 个政治人物姓名的列表,找到字符串中的每个实例并将其包装在span.

优先级是具有不断增长的名称列表的性能,以及确定例如“Tony Blair”、“Tony Blair III”之间差异的准确性。

例如,这个:

["Tony Blair", "Margaret Thatcher", "Tony Blairite", "Tony Blair III", etc...]
"The best PM after Tony Blair was Margaret Thatcher."

变成:

"The best PM after <span class="mp">Tony Blair</span> was <span class="mp">Margaret Thatcher</span>."
4

1 回答 1

0
var names = ['foo','bar'];
var content = "this foo is bar foobar  foo ";
for (var c=0,l=names.length;c<l;c++) {
  var r = new RegExp("\\b("+names[c]+")\\b","gi");
  content = content.replace(r,'<span class="mp">$1</span>');
}
于 2013-05-08T00:08:08.223 回答