【发布时间】:2011-06-04 20:13:41
【问题描述】:
我有一个 c# 函数,可以将所有非英语字符转换为给定文本的正确字符。如下所示
public static string convertString(string phrase)
{
int maxLength = 100;
string str = phrase.ToLower();
int i = str.IndexOfAny( new char[] { 'ş','ç','ö','ğ','ü','ı'});
//if any non-english charr exists,replace it with proper char
if (i > -1)
{
StringBuilder outPut = new StringBuilder(str);
outPut.Replace('ö', 'o');
outPut.Replace('ç', 'c');
outPut.Replace('ş', 's');
outPut.Replace('ı', 'i');
outPut.Replace('ğ', 'g');
outPut.Replace('ü', 'u');
str = outPut.ToString();
}
// if there are other invalid chars, convert them into blank spaces
str = Regex.Replace(str, @"[^a-z0-9\s-]", "");
// convert multiple spaces and hyphens into one space
str = Regex.Replace(str, @"[\s-]+", " ").Trim();
// cut and trim string
str = str.Substring(0, str.Length <= maxLength ? str.Length : maxLength).Trim();
// add hyphens
str = Regex.Replace(str, @"\s", "-");
return str;
}
但我应该在客户端使用与 javascript 相同的功能。 上面的函数可以转成js吗?
【问题讨论】:
-
一个更好的标题可能是:如何使用 JavaScript 将非英文字符转换为英文
-
是的,克里斯,你是对的。我已经改了。
标签: c# javascript regex