简体   繁体   English

使用子字符串方法突出显示字符串中的文本

[英]Highlighting a text from string by using sub-string method

I have a string which you can is like: 我有一个字符串,你可以像这样:

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum只是印刷和排版行业的伪文本。 Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. 自1500年代以来,Lorem Ipsum一直是行业的标准伪文本,当时一位不知名的打印机拿起一个厨房,将其打乱成一本样本书。 It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged 它不仅存活了五个世纪,而且还经历了电子排版的飞跃,基本上保持不变

Now, in this string, I do have an json object in which from back-end all the start and end offsets of string which I have to highlight. 现在,在此字符串中,我确实有一个json对象,在该对象中,后端必须突出显示字符串的所有起始和结束偏移量。

Now, for highlighting I am using following logic: 现在,为了突出显示,我使用以下逻辑:

$scope.highlightHTML = function(content, startoffset, endoffset) {
  var className = 'mark';
  console.log(content.substring(startoffset, endoffset));
  return content.replace(content.substring(startoffset, endoffset), '<span class="' + className + '">$&</span>');
}

Here content is the given string and start and end are the endoffsets of the highlighting string. 这里的内容是给定的字符串,而start和end是突出显示字符串的endoffsets。

Now, while calling this: 现在,在调用时:

jsonDataArray.forEach(function(item) {
  responseData = $scope.highlightHTML(responseData, item.startOffset, item.endOffset, item.color);
});
$rootScope.data.htmlDocument = responseData.replace(/\n/g, "</br>");

Here consider responseData is the string which I provided in the question. 这里考虑responseData是我在问题中提供的字符串。 From this I am calling the highlighthtml function. 由此,我将调用Highlighthtml函数。

Here the problem is that I am replacing the string with the span tags. 这里的问题是我用span标签替换了字符串。 Now what happens in loop is that when first lets say in array first value to highlight is the printing and typesetting industry. 现在循环发生的事情是,当首先让我们在数组中说出第一个要突出的值时,就是the printing and typesetting industry. . So, offsets of this I get from the backend. 因此,这是我从后端获得的补偿。 Now, it will highlight that by replacing with the spans. 现在,它将通过替换跨度来突出显示这一点。

Now, when in array for second value, let's say a galley of type and scrambled it I get offsets but when it goes in highlight function then it is not getting the exact string because we have changed that string just by adding the span so now the offsets are changed for that string. 现在,当在数组中获取第二个值时,假设a galley of type and scrambled it我得到了偏移量,但是当它进入高亮功能时,它并没有得到确切的字符串,因为我们只是通过添加跨度来更改了该字符串,所以现在该字符串的偏移量已更改。 So, because of this I am not able to highlight the proper words. 因此,因此,我无法突出显示正确的词。

You could go through you replacements from back to front by using a reversed loop to ensure the offsets of the elements, which aren't precessed yet aren't changed in the loop. 您可以使用反向循环来确保从头到尾进行替换,以确保元素的偏移量(在循环中尚未进行处理)没有变化。

I added the sorting method as well, if you aren't sure if your data is in the correct order. 如果不确定数据的顺序是否正确,我还添加了排序方法。

 $scope.highlightHTML = function(content, startoffset, endoffset) { var className = 'mark'; console.log(content.substring(startoffset, endoffset)); return content.replace(content.substring(startoffset, endoffset), '<span class="' + className + '">$&</span>'); } //Only if you don't know if they are in the correct order: jsonDataArray = jsonDataArray.sort((a, b) => a.startOffset - b.startOffset); for (var i = jsonDataArray.length - 1; i >= 0; i--) { const item = jsonDataArray[i]; responseData = $scope.highlightHTML(responseData, item.startOffset, item.endOffset, item.color); }; $rootScope.data.htmlDocument = responseData.replace(/\\n/g, "</br>"); 

You can update the offsets by adding the extra length for the spans text added, in your loop like this: 您可以通过在循环中为添加的跨度文本添加额外的length来更新offsets ,如下所示:

var length = '<span class="mark"></span>'.length:
jsonDataArray.forEach(function(item, index) {
    responseData = $scope.highlightHTML(responseData, item.startOffset + (index * length), item.endOffset + (index * length), item.color);
});
$rootScope.data.htmlDocument = responseData.replace(/\n/g, "</br>");

So here in each iteration, you will be incrementing both offsets with (index *length) , so for the first iteration you won't update them because index is 0 , then you will increment it by 1*length and so on. 因此在这里,在每次迭代中,您都将使用(index *length)来增加两个偏移量,因此对于第一次迭代,您将不会更新它们,因为index0 ,然后将其增加1*length等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM