突出显示字符串中的多个关键字,忽略C#中添加的HTML

时间:2018-05-30 12:39:43

标签: c# html regex

我有一个扩展名,循环遍历字符串,以查找任意数量的关键字(或搜索字词)的所有实例。当找到匹配项时,会在每个关键字周围添加一个span标记,以突出显示的关键字。

        public static string HighlightKeywords( this string input, string keywords )
    {
        if( input == String.Empty || keywords == String.Empty )
        {
            return input;
        }

        string[] words = keywords.Split( new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries );

        foreach( string word in words )
        {
            input = Regex.Replace( input, word, string.Format( "<span class=\"highlight\">{0}</span>", "$0" ), RegexOptions.IgnoreCase );
        }
        return input;
    }

除非您使用与添加的范围标记匹配的搜索字词,否则该方法效果很好。

狡猾的输出示例:

  

字符串“The class is high”

     

关键字:“class high”

     

产生的狡猾的HTML输出:input =“&lt; span class ='highlight'&gt; classspan&gt;很高”

因此,查找原始字符串中的第一个关键字,添加装饰HTML,然后在更改的字符串中查找下一个关键字,添加更多HTML并造成混乱。

在搜索每个关键字时,有没有办法避免使用装饰关键字?

更新:

鉴于案例不敏感很重要,我探讨了各种不区分大小写的替换方法,并取得了部分成功。搜索功能通过忽略大小写来工作,但返回了关键字中使用的大小写并将其替换为原始文本,例如搜索“HIGH”返回“Class is HIGH”。这看起来很糟糕。

所以,我回到使用Regex(叹气)。我设法重写我的扩展如下,这似乎工作得很好,但我想知道这个扩展真的有多高效。我欢迎任何关于改进此代码或在没有正则表达式的情况下实现此目的的意见。

    public static string HighlightKeywords( this string input, string keywords, string classname )
    {
        if( input == String.Empty || keywords == String.Empty )
        {
            return input;
        }

        string[] words = keywords.Split( new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries );

        foreach( string word in words )
        {
            input = Regex.Replace( input, Regex.Escape( word ), string.Format( "<!--{0}-->", Regex.Unescape( "$0" ) ), RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled );
        }

        var s = new StringBuilder( );
        s.Append( input );
        s.Replace( "<!--", "<span class='" + classname + "'>" ).Replace( "-->", "</span>" );

        return s.ToString( );
    }

2 个答案:

答案 0 :(得分:1)

尝试这个简单的改变:

public static string HighlightKeywords(this string input, string keywords)
{
    if (input == String.Empty || keywords == String.Empty)
    {
        return input;
    }

    return Regex.Replace(
        input,
        String.Join("|", keywords.Split(' ').Select(x => Regex.Escape(x))),
        string.Format("<span class=\"highlight\">{0}</span>", "$0"),
        RegexOptions.IgnoreCase);
}

Regex为您完成工作。

根据您的输入"The class is high".HighlightKeywords("class high"),您会获得"The <span class="highlight">class</span> is <span class="highlight">high</span>"

答案 1 :(得分:-1)

略有不同的方法。 添加StringBuilder会更好!

    {
  "Version":"2012-10-17",
  "Statement": [
    {
       "Sid": "AllowAllS3ActionsInOtherFolders",
       "Action":["s3:*"],
       "Effect":"Allow",
       "Resource": ["arn:aws:s3:::my-bucket"],
       "Condition":{"StringLike":{"s3:prefix":
                  [
                       "backup/media/*"
                       "backup/static/*"
                  ]
               }
        }
    }
  ]
}
相关问题