如何通过textparagraph大胆特定的词(一个传递的参数) [英] How to bold a specific word(a passed parameter) in passed textparagraph

查看:124
本文介绍了如何通过textparagraph大胆特定的词(一个传递的参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欲粗体一个特定字(它是一个传递的参数)在另一传递的参数是一个文本段落。

I want to bold a specific word (which is a passed parameter) in another passed parameter which is a text paragraph.

所以,在所有什么我做的是:
- 搜索这个词可以说帮助中的一段我卡住了,请帮助我
- 当在段落中找到这个词的帮助,然后将其替换为帮助,使该段落变为我坚持,请帮助我。我一直在努力的正则表达式前pressions做,但无法弄清楚这个问题。

So over all what I am doing is : - search the word lets say "help" in the paragraph " I am stuck , please help me" - and when this word "help" is found in the paragraph then replace it with help so that the paragraph changes to " I am stuck , please help me" . I have been trying regex expressions to do but unable to figure out the problem.

HTML code:

<div>
      {{ boldText(value.search_keywords, value.url.textRecieved) }}
</div>

AngularJS code:

$scope.boldText=function(searchKeywordsList, textParagraph){
        var final_textParagraph;
        for (var index=0; index<searchKeywordsList.length; index++){
            if (textParagraph.indexOf(searchKeywordsList[index]) > -1) {
                var regex = new RegExp('\\*(.+?)\\*', 'g');
                var replace = '<b>$1</b>';
                var searchedKeyword = searchKeywordsList[index].replace(searchKeywordsList[index], '*'+searchKeywordsList[index]+'*');
               /* console.log(searchedKeyword);*/
                var boldValue = searchedKeyword.replace(regex, replace);
                console.log(boldValue);
                final_textParagraph = textParagraph.replace(searchKeywordsList[index], boldValue);
            }
            else{
                final_textParagraph = textParagraph;
            }
        }
        return final_textParagraph;
    };

电流输出:


  • 搜索关键词:

收益,
网络


  • 网站数据:

...安全性),最近担任副总裁形状安全全球销售。在此之前,Rotolo担任销售副总裁 - 西在帕洛阿尔托网络,在那里他帮助从零增长收入超过3.5亿$,每年

...security) and most recently served as VP of Worldwide Sales at Shape Security. Prior to that Rotolo served as VP of Sales - West at Palo Alto Networks, where he helped grow revenues from zero to over $350 million per year.

预期输出:


  • 搜索关键词:

收益,
网络


  • 网站数据:

...安全性),最近担任副总裁形状安全全球销售。在此之前,Rotolo担任销售副总裁 - 西在帕洛阿尔托网络 S,在那里他帮助从零成长收入 s到超过3.5亿$,每年

...security) and most recently served as VP of Worldwide Sales at Shape Security. Prior to that Rotolo served as VP of Sales - West at Palo Alto Networks, where he helped grow revenues from zero to over $350 million per year.

我在做什么错了?

推荐答案

您需要包括ngSanitize和使用NG绑定,HTML,如果要注入的标记这样。也请不要使用&LT; B&GT; 元素,而是适用于跨度与大胆的风格像&LT;跨度类=大胆&GT; &LT; / SPAN&GT;

You need to include ngSanitize and use ng-bind-html if you want to inject markup like that. And also please dont use <b> element, but instead apply a span with a bold style like <span class="bold"></span>

用法

<ANY
  ng-bind-html="">
...
</ANY>

在你的情况

<div ng-bind-html="boldText(value.search_keywords, value.url.textRecieved)"></div>

请确保包括ngSanitize
了解更多关于NG-绑定-HTML: https://docs.angularjs.org/api/纳克/指令/ ngBindHtml

Make sure to include ngSanitize Read more about ng-bind-html: https://docs.angularjs.org/api/ng/directive/ngBindHtml

这篇关于如何通过textparagraph大胆特定的词(一个传递的参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆