简体   繁体   English

角度过滤器,用于将asterik内部的单词突出显示为粗体

[英]Angular filter for highlighting word(s) inside of asterik to with bold

I want to input * this is a sentence * and get < strong >this is a sentence< /strong > as an output. 我想输入*这是一个句子*并获得<strong>这是一个句子</ strong>作为输出。

I have looked at https://github.com/Hypercubed/angular-marked but this gives too many options that I don't want (like headings). 我看过https://github.com/Hypercubed/angular-marked,但这提供了我不想要的太多选项(如标题)。

Any help? 有什么帮助吗?

I've created a demo filter to convert text to <strong>text</strong> here: http://plnkr.co/edit/cQjytfvsT1Qu9ygjfEHz?p=preview 我在此处创建了一个演示过滤器,用于将文本转换为<strong>text</strong>http : //plnkr.co/edit/cQjytfvsT1Qu9ygjfEHz?p=preview

app.filter('asteriskToBoldFilter', function($sce){
  return function(val) {
    var matches = val.match( /\*(.*?)\*/g );
    if(matches){
      matches.forEach(function(line){
        var newline = line.replace('*', '<strong>').replace('*', '</strong>');
        val = val.replace(line, newline);
      })
    }
    return $sce.trustAsHtml(val);
  };
})

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

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