如何用数组列表中的搜索键替换文本

问题描述 投票:1回答:1

我对搜索文字highlight有疑问

假设我有这样的搜索文字列表var list = ['Alexander','&','2009','>'];

我要highlight ['Alexander','&','2009','>'];中的所有文本

问题: 1)我想突出显示list 2)中的所有关键字,我想避免使用for loop

这是我尝试过的:

var list = ['Alexander','&','2009','>']; // comes dynamically

var textDescription = `Alexander the Great (Ancient Greek: Ἀλέξανδρος ὁ Μέγας, romanized: Aléxandros ho Mégas), was a king (basileus) of the ancient Greek kingdom of Macedon[a] and a member of the Argead dynasty. He was born in Pella in 356 BC and succeeded his father Philip II to the throne at the age of 20. He spent most of his ruling years on an unprecedented military campaign through Asia and northeast Africa, and by the age of thirty, he had created one of the largest empires of the ancient world, stretching from Greece to northwestern India.[1][2] He was undefeated in battle and is widely considered one of history's most successful military commanders.[3]

During his youth, Alexander was tutored by Aristotle until age 16. After Philip's assassination in 336 BC, he succeeded his father to the throne and inherited a strong kingdom `;

for(var i = 0; i < list.length; i++){
   var searchText = list[i];
   var pattern = new RegExp(`\\b${searchText}(?!([\w\s\.]*<\/highlight>))`,'ig');
   var res = textDescription.replace(pattern,function(x){
       return '<highlight>'+x+'</highlight>';
   });
   textDescription = res;
}

$('#highlight').html(textDescription);
highlight{
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="highlight"></div>

[注:我想避免像下面这样嵌套]

   <highlight>Alexander<highlight>Alexander</highlight></highlight>
javascript jquery regex
1个回答
0
投票
[您会看到我在所放内容的末尾添加了一些文本,以便可以测试所有字符串,并且还可以在其他字符串中间找到它们时(例如, 2009年为20090。

var list = ['Alexander','&','2009','>']; // comes dynamically var textDescription = `Alexander the Great (Ancient Greek: Ἀλέξανδρος ὁ Μέγας, romanized: Aléxandros ho Mégas), was a king (basileus) of the ancient Greek kingdom of Macedon[a] and a member of the Argead dynasty. He was born in Pella in 356 BC and succeeded his father Philip II to the throne at the age of 20. He spent most of his ruling years on an unprecedented military campaign through Asia and northeast Africa, and by the age of thirty, he had created one of the largest empires of the ancient world, stretching from Greece to northwestern India.[1][2] He was undefeated in battle and is widely considered one of history's most successful military commanders.[3] During his youth, Alexander was tutored by Aristotle until age 16. After Philip's assassination in 336 BC, he succeeded his father to the throne and inherited a strong kingdom in 2009 BC & 2010 BC. Alexander is > 20090 people.` var pattern = new RegExp(`(?<=(^|\\W))(${list.join('|')})(?=(\\W|$))`, 'ig') var textDescription = textDescription.replace(pattern, '<highlight>$2</highlight>') $('#highlight').html(textDescription)

highlight { color: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="highlight"></div>
© www.soinside.com 2019 - 2024. All rights reserved.