Javascript 使用或不使用正则表达式查找并替换c#中的多个字符串
我需要一些代码方面的帮助。我试图做的是在一个句子中一次找到一个字符串,并用span标记替换同一个字符串 我已经用javascript实现了同样的效果,但我不确定如何在C# js中的代码:Javascript 使用或不使用正则表达式查找并替换c#中的多个字符串,javascript,c#,regex,str-replace,Javascript,C#,Regex,Str Replace,我需要一些代码方面的帮助。我试图做的是在一个句子中一次找到一个字符串,并用span标记替换同一个字符串 我已经用javascript实现了同样的效果,但我不确定如何在C# js中的代码: //this function takes 'buffer' as content & //search query (multiple string values) as replaceStrings and highlights the query function highlitor(buff
//this function takes 'buffer' as content &
//search query (multiple string values) as replaceStrings and highlights the query
function highlitor(buffer, replaceStrings)
{
var highlightedText = buffer;
for (var i = 0; i < replaceStrings.length; i++)
{
var exp = new RegExp("(" + replaceStrings[i] + ")", "gi");
highlightedText = highlightedText.replace(exp, "<span class='highlight-search-text'>$1</span>");
}
return highlightedText;
}
//此函数将'buffer'作为内容&
//将查询(多个字符串值)搜索为替换字符串并高亮显示查询
函数highlitor(缓冲区、替换字符串)
{
var highlightedText=缓冲区;
for(var i=0;i
比如说
buffer=" This is an exciting and enhansive test" replaceStrings=[exciting,enhace];
highlightedText="This is an <span class='highlight-text'>exciting</span> and <span class='highlight-text'>enhansive</span> test"
buffer=“这是一个激动人心的增强型测试”replaceStrings=[激动人心的,增强型];
highlightedText=“这是一项激动人心的测试”
提前谢谢。我想你只需要在一个循环中:
public static string Highlitor(string text, IEnumerable<string> replaceStrings)
{
string result = text;
foreach(string repl in replaceStrings.Distinct())
{
string replWith = string.Format("<span class='highlight-text'>{0}</span>", repl);
result = result.Replace(repl, replWith);
}
return result;
}
以下是一种不区分大小写替换字符串并支持保留原始大小写的方法:
public static string ReplaceCaseInsensitive(string original, string pattern, string replacement, bool keepOriginalCase = false)
{
int count, position0, position1;
count = position0 = position1 = 0;
int replacementIndexOfPattern = replacement.IndexOf(pattern, StringComparison.OrdinalIgnoreCase);
if (replacementIndexOfPattern == -1)
keepOriginalCase = false; // not necessary
int inc = (original.Length / pattern.Length) *
(replacement.Length - pattern.Length);
char[] chars = new char[original.Length + Math.Max(0, inc)];
bool[] upperCaseLookup = new bool[pattern.Length];
while ((position1 = original.IndexOf(pattern, position0, StringComparison.OrdinalIgnoreCase)) != -1)
{
// first part that will not be replaced
for (int i = position0; i < position1; ++i)
chars[count++] = original[i];
// remember the case of each letter in the found patter that will be replaced
if (keepOriginalCase)
{
for (int i = 0; i < pattern.Length; ++i)
upperCaseLookup[i] = Char.IsUpper(original[position1 + i]);
}
// The part that will be replaced:
for (int i = 0; i < replacement.Length; ++i)
{
// only keep case of the relevant part of the replacement that contains the part to be replaced
bool lookupCase = keepOriginalCase
&& i >= replacementIndexOfPattern
&& i < replacementIndexOfPattern + pattern.Length;
char newChar = replacement[i];
if (lookupCase)
{
bool wasUpper = upperCaseLookup[i - replacementIndexOfPattern];
bool isUpper = Char.IsUpper(newChar);
if (wasUpper && !isUpper)
newChar = Char.ToUpperInvariant(newChar);
else if (!wasUpper && isUpper)
newChar = Char.ToLowerInvariant(newChar);
}
else
{
newChar = replacement[i];
}
chars[count++] = newChar;
}
position0 = position1 + pattern.Length;
}
if (position0 == 0)
return original;
// the rest
for (int i = position0; i < original.Length; ++i)
chars[count++] = original[i];
return new string(chars, 0, count);
}
public static string replaceCase不敏感(string-original、string-pattern、string-replacement、bool-keepOriginalCase=false)
{
整数计数,位置0,位置1;
计数=位置0=位置1=0;
int replacementIndexOfPattern=replacement.IndexOf(模式,StringComparison.OrdinalIgnoreCase);
if(replacementIndexOfPattern==-1)
keepOriginalCase=false;//不需要
int inc=(original.Length/pattern.Length)*
(替换.长度-图案.长度);
char[]chars=新字符[original.Length+Math.Max(0,inc)];
bool[]upperCaseLookup=新bool[pattern.Length];
while((position1=original.IndexOf(pattern,position0,StringComparison.OrdinalIgnoreCase))!=-1)
{
//不可更换的第一部分
对于(int i=位置0;i<位置1;++i)
字符[count++]=原始[i];
//记住将被替换的已找到模式中每个字母的大小写
if(keepOriginalCase)
{
对于(int i=0;i=替换索引模式
&&i
它的算法基于:。我得到的功能如下:
for (var j = 0; j < searchTermArray.Length; j++)
{
CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;
int indexOfTerm = ci.IndexOf(text, searchTermArray[j], CompareOptions.IgnoreCase);
//dont replace if there is no search term in text
if(indexOfTerm!= -1)
{
string subStringToHighlight = text.Substring(indexOfTerm, searchTermArray[j].Length);
//replacing with span
string replaceWith = string.Format("<span class='highlight-search-text'>{0}</span>", subStringToHighlight);
text = Regex.Replace(text, searchTermArray[j], replaceWith, RegexOptions.IgnoreCase);
}
}
for(var j=0;j
等待您如何知道基于增强
替换增强
?您还需要从此方法返回文本。这需要返回类型。可能应该解释如何调用它(不确定OP是否理解IEnumerable
)。@Tim我忘了提到字符串可以是重复的..f.e句子可以有“令人兴奋的”任何时间。@Prathamesdhanawade:我知道。也许可以简化,但我还没见过如何简化。但这项任务没有简单、简洁或可读的方式。至少在一个有意义名称的方法中隐藏了复杂性。@Prathamesdhanawade:fyi,我编辑了一点,但是。现在它不再简单了,但如果不需要,它也不会增加/减少字符。
for (var j = 0; j < searchTermArray.Length; j++)
{
CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;
int indexOfTerm = ci.IndexOf(text, searchTermArray[j], CompareOptions.IgnoreCase);
//dont replace if there is no search term in text
if(indexOfTerm!= -1)
{
string subStringToHighlight = text.Substring(indexOfTerm, searchTermArray[j].Length);
//replacing with span
string replaceWith = string.Format("<span class='highlight-search-text'>{0}</span>", subStringToHighlight);
text = Regex.Replace(text, searchTermArray[j], replaceWith, RegexOptions.IgnoreCase);
}
}