忽略HTML标签以突出显示匹配的单词

哈拉巴

在突出显示与搜索输入匹配的单词后,我想用新模板替换HTML模板内容。

如摘要所示,我正在使用高亮过滤器,该过滤器通过添加来向匹配的单词添加黄色背景&zwj;<span class="highlighted">$1&zwj;</span>

问题一

问题是,有时突出显示无法执行,并且我不知道为什么,例如,当搜索时accountin,则突出显示,但是当搜索时accounting,却未突出显示。

仅当匹配的单词在诸如<bold>和的某些子标签中时,才会出现此问题<tag>,您可以尝试搜索,accounting然后您会明白我的意思。为什么会这样?以及如何解决?

问题二

在HTML中,我使用的是诸如的实质性图标<md-icon class="material-icons ltr">folder</md-icon>,并且当搜索时folder,突出显示将出现在该图标上。

即使我像svg一样使用图标<md-icon md-svg-src="path/user.svg"></md-icon>,也会发生相同的问题。

那么,有什么办法可以避免这种情况?

angular.module("myApp", ["ngMaterial"])
.filter('highlight', function ($sce) {
        function mapText(text,tag,tagvalue){
            var reg = new RegExp("[\>][^\<\>.]*"+tag+"[^\<\>.][\<]*","gi");
            var result = text.replace(reg,function(item,exp){               
                var subRegex = new RegExp(tag,"gi");
                return item.replace(subRegex,tagvalue);
            });
            return result;
        }
        return function (text, searchSrting) {
            if(searchSrting){
                searchSrting = searchSrting.split(/\s+/);
                if(typeof text !== "undefined") {
                        for (var i = 0; i < searchSrting.length; i++) {
                            if(searchSrting[i]==""){
                                continue;
                            }
                            else{
                                var tagvalue = '&zwj;<span class="highlighted">' + searchSrting[i] + '&zwj;</span>';
                                text = mapText(text, searchSrting[i], tagvalue);
                            }
                        }
                }
                return $sce.trustAsHtml(text)
            }
        }
    })
.controller("main", function($scope){ 
  $scope.searchString="";
  $scope.content="<module> <ti-tle>User Management</ti-tle><br><tag-group><tag>User Management</tag></tag-group><info-group><info><md-icon class='material-icons ltr'>perm_identity</md-icon>published by: Ha ba</info>        <info><md-icon class='material-icons ltr'>folder</md-icon>User Management</info><info><md-icon class='material-icons ltr'>publish</md-icon>published: 25 May 2016</info></info-group><hr>In <bold>AMe</bold>, you can manage multiple bank accounts <br><br>    <sub-title>        Introduction  Accounting</sub-title>    The Sales Planner is a useful step-by-step guide created to help you implement your sales funnel    <br>Accounting    Go to <bold>Accounting</bold> ‣ <bold>Configuration</bold> ‣ <bold>Bank Accounts</bold> and click on the Bank item. Edit it        <note><md-icon class='material-icons'>error_outline</md-icon>         will detect the bank account type (e.g. IBAN) to allow some payment method like SEPA.    </note>    <br><br>    <sub-title>        Set up your first sales team    </sub-title>    For example, if within your company Tim is selling products and John is selling maintenance contracts, they will be assigned to different teams and will only receive opportunities that make sense to them.    <br><br>        <sub-title>        Set up incoming email to generate opportunities    </sub-title>    In CRM, one way to generate opportunities into your sales team is to create a generic email address as a trigger.    </module>";
})
module{
        font-size: 14px;
        color: #484848;
    }
    ti-tle {
        font-size: x-large;
        color: rgb(50, 118, 177);
        display: block;
        font-weight: bold;
    }
    tag-group{
        display: block;
        line-height: 3;
    }
    tag{
        background-color: #daebe8;
        padding:2px 6px;
        font-weight: bold;
        font-size: 12px;
        margin: 2px;
        border-radius: 4px;
        cursor: pointer;
        color: #667292;
    }
    tag:hover{
        background-color: #87bdd8;
    }
    info-group{display: block}
    info{
        color: gray;
        margin: 4px;
        font-size: 12px;
    }
    sub-title{
        font-weight: bold;
        font-size: 18px;
        display: block;
        line-height: 2;
    }
    img{
        display: block;
        margin: 30px 0;
        width: 100%;
    }
    bold{
        font-weight: bold;
    }
    note{
        background-color: antiquewhite;
    }
    .highlighted {
        background: yellow;
    }
    md-icon{direction: ltr}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.7/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.7/angular-animate.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.7/angular-aria.js"></script>
<script src="//rawgit.com/angular/bower-material/master/angular-material.js"></script>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
      rel="stylesheet">

<div ng-app="myApp">
<div ng-controller="main">
  <label>search</label>
  <input ng-model="searchString"/>
  <div ng-if="!searchString">
<module>
    <ti-tle>User Management</ti-tle>
    <br>
    <tag-group>
        <tag>User Management</tag>
    </tag-group>
    <info-group>
        <info><md-icon class="material-icons ltr">perm_identity</md-icon>published by: Ha ba</info>
        <info><md-icon class="material-icons ltr">folder</md-icon>User Management</info>
        <info><md-icon class="material-icons ltr">publish</md-icon>published: 25 May 2016</info>
    </info-group>
    <hr>

    In <bold>AMe</bold>, you can manage multiple bank accounts
    <br><br>
    <sub-title>
        Introduction Accounting
    </sub-title>
    The Sales Planner is a useful step-by-step guide created to help you implement your sales funnel
    <br>
  Accounting
    Go to <bold>
  Accounting</bold> ‣ <bold>Configuration</bold> ‣ <bold>Bank Accounts</bold> and click on the Bank item. Edit it
    <note><md-icon class="material-icons">error_outline</md-icon>
       will detect the bank account type (e.g. IBAN) to allow some payment method like SEPA.
    </note>
    <br><br>
    <sub-title>
        Set up your first sales team
    </sub-title>
    For example, if within your company Tim is selling products and John is selling maintenance contracts, they will be assigned to different teams and will only receive opportunities that make sense to them.


    <br><br>
    <sub-title>
        Set up incoming email to generate opportunities
    </sub-title>
    In  CRM, one way to generate opportunities into your sales team is to create a generic email address as a trigger. For example, if
</module>

  
  </div>
  <div ng-if="searchString"  ng-bind-html="content | highlight:searchString"></div>

</div>
</div>

信任阿尔通塔斯

首先,对于快速测试的答案,我感到抱歉。

这是您的答案:

var reg = new RegExp("[\>][^\<\>.]*"+tag+"[^\<\>.][\<]*","gi");

在此行中,正则表达式搜索任何字符,但不搜索'> <'。这就是为什么您的代码工作不正确正确的代码在这里:

var reg = new RegExp("[\>][^\<\>.]*"+tag+"[^\<\>.]*[\<]*","gi");

第2部分:在此代码中,您将一个接一个地替换单词。因此,当您替换一个单词为第二个单词时,该单词正则表达式将不匹配。例子:

我们的重点词是火和消防员。

var text = "<div>fireman has killed fire</div>";

在第一个循环之后,文本将如下所示:

text = "<div><highlight>fire</highlight>man has killed <highlight>fire</highlight></div>";

因此我们的正则表达式在此新文本中将与第二个单词不匹配。

但是如果我们按文本长度对数组进行排序。这个问题将得到解决。

<md-icon class="material-icons ltr">folder</md-icon>情况看起来像这样的正则表达式不会帮助你。如果要在项目中使用正则表达式来执行此操作,则可以在正则表达式中添加所有案例。

但是像这样在正则表达式中的特殊用法……我认为这不是最好的方法。

因为; 将来如果您决定更改图标库。您也必须更改您的正则表达式。正则表达式不是首先看起来可以理解的语言。最后,这将是非常非常糟糕的隐藏业务。

希望您能找到想要的东西。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何突出显示html标签内的单词而不突出显示vue中的标签

来自分类Dev

如何在Emacs中突出显示不匹配的HTML标签?

来自分类Dev

使用 jQuery 在 textarea 中匹配和突出显示标签/单词(更改颜色和字体粗细)

来自分类Dev

XHTML:如何突出显示匹配标签?

来自分类Dev

Notepad ++默认匹配标签突出显示

来自分类Dev

如何启用或禁用突出显示匹配标签

来自分类Dev

借助Elasticsearch,我可以针对不同的匹配标记用不同的HTML标签突出显示吗?

来自分类Dev

HTML悬停时突出显示标签

来自分类Dev

使用ReactJ在搜索列表中突出显示匹配的字母/单词

来自分类Dev

在数据框Pandas HTML中突出显示某些单词

来自分类Dev

使用JavaScript,HTML和CSS突出显示单词

来自分类Dev

PHP突出显示html标签whitin加载html内容

来自分类Dev

修改HTML标签的Sublime文本语法突出显示

来自分类Dev

HTML-SELECT标签的OPTION部分突出显示文本

来自分类Dev

修改HTML标签的Sublime文本语法突出显示

来自分类Dev

HTML-SELECT标签的OPTION部分突出显示文本

来自分类Dev

突出显示RichEditBox中的单词

来自分类Dev

如何突出显示重复的单词?

来自分类Dev

如何删除突出显示的单词?

来自分类Dev

用@符号匹配单词并忽略

来自分类Dev

从文本输入中突出显示div中所有匹配的单词

来自分类Dev

在记事本中更改突出显示颜色以匹配单词

来自分类Dev

jQuery UI自动完成功能突出显示完整单词,而不是匹配的子字符串

来自分类Dev

PHP-如何突出显示所有单词(utf-8)匹配项

来自分类Dev

将输入文本与字符串数组进行比较后,突出显示结果中匹配的单词

来自分类Dev

输入替换命令时,vim可以突出显示当前匹配的单词吗?

来自分类Dev

通过仅匹配整个单词来突出显示单元格(而不是单元格)中的单词?

来自分类Dev

如何突出显示所选标签?

来自分类Dev

突出显示当前标签页?

Related 相关文章

  1. 1

    如何突出显示html标签内的单词而不突出显示vue中的标签

  2. 2

    如何在Emacs中突出显示不匹配的HTML标签?

  3. 3

    使用 jQuery 在 textarea 中匹配和突出显示标签/单词(更改颜色和字体粗细)

  4. 4

    XHTML:如何突出显示匹配标签?

  5. 5

    Notepad ++默认匹配标签突出显示

  6. 6

    如何启用或禁用突出显示匹配标签

  7. 7

    借助Elasticsearch,我可以针对不同的匹配标记用不同的HTML标签突出显示吗?

  8. 8

    HTML悬停时突出显示标签

  9. 9

    使用ReactJ在搜索列表中突出显示匹配的字母/单词

  10. 10

    在数据框Pandas HTML中突出显示某些单词

  11. 11

    使用JavaScript,HTML和CSS突出显示单词

  12. 12

    PHP突出显示html标签whitin加载html内容

  13. 13

    修改HTML标签的Sublime文本语法突出显示

  14. 14

    HTML-SELECT标签的OPTION部分突出显示文本

  15. 15

    修改HTML标签的Sublime文本语法突出显示

  16. 16

    HTML-SELECT标签的OPTION部分突出显示文本

  17. 17

    突出显示RichEditBox中的单词

  18. 18

    如何突出显示重复的单词?

  19. 19

    如何删除突出显示的单词?

  20. 20

    用@符号匹配单词并忽略

  21. 21

    从文本输入中突出显示div中所有匹配的单词

  22. 22

    在记事本中更改突出显示颜色以匹配单词

  23. 23

    jQuery UI自动完成功能突出显示完整单词,而不是匹配的子字符串

  24. 24

    PHP-如何突出显示所有单词(utf-8)匹配项

  25. 25

    将输入文本与字符串数组进行比较后,突出显示结果中匹配的单词

  26. 26

    输入替换命令时,vim可以突出显示当前匹配的单词吗?

  27. 27

    通过仅匹配整个单词来突出显示单元格(而不是单元格)中的单词?

  28. 28

    如何突出显示所选标签?

  29. 29

    突出显示当前标签页?

热门标签

归档