[英]How to Convert Non-English Characters to English Using JavaScript
I have ac# function which converts all non-english characters to proper characters for a given text. 我有一个ac#函数,它将所有非英文字符转换为给定文本的正确字符。 like as follows 如下
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;
}
but i should use same function on client side with javascript. 但我应该使用javascript在客户端使用相同的功能。 is it possible to convert above function to js ? 是否有可能将上述函数转换为js?
This should be what you are looking for - check the demo to test. 这应该是你正在寻找的 - 检查演示测试。
function convertString(phrase)
{
var maxLength = 100;
var returnString = phrase.toLowerCase();
//Convert Characters
returnString = returnString.replace(/ö/g, 'o');
returnString = returnString.replace(/ç/g, 'c');
returnString = returnString.replace(/ş/g, 's');
returnString = returnString.replace(/ı/g, 'i');
returnString = returnString.replace(/ğ/g, 'g');
returnString = returnString.replace(/ü/g, 'u');
// if there are other invalid chars, convert them into blank spaces
returnString = returnString.replace(/[^a-z0-9\s-]/g, "");
// convert multiple spaces and hyphens into one space
returnString = returnString.replace(/[\s-]+/g, " ");
// trims current string
returnString = returnString.replace(/^\s+|\s+$/g,"");
// cuts string (if too long)
if(returnString.length > maxLength)
returnString = returnString.substring(0,maxLength);
// add hyphens
returnString = returnString.replace(/\s/g, "-");
alert(returnString);
}
Edit: Updated the demo to add for testing of input. 编辑:更新了要添加的演示以测试输入。
function convertString(phrase)
{
var maxLength = 100;
var str = phrase.toLowerCase();
var charMap = {
'ö': 'o',
'ç': 'c',
'ş': 's',
'ı': 'i',
'ğ': 'g',
'ü': 'u'
};
var rx = /(ö|ç|ş|ı|ğ|ü)/g;
// if any non-english charr exists,replace it with proper char
if (rx.test(str)) {
str = str.replace(rx, function(m, key, index) {
return charMap[key];
});
}
// if there are other invalid chars, convert them into blank spaces
str = str.replace(/[^a-z\d\s-]/gi, "");
// convert multiple spaces and hyphens into one space
str = str.replace(/[\s-]+/g, " ");
// trim string
str.replace(/^\s+|\s+$/g, "");
// cut string
str = str.substring(0, str.length <= maxLength ? str.length : maxLength);
// add hyphens
str = str.replace(/\s/g, "-");
return str;
}
It's certainly possible to convert it... 转换它当然是可能的......
ToLower -> toLowerCase, Replace => replace, Length => length ToLower - > toLowerCase,Replace => replace,Length => length
You'd have to code up IndexOfAny, but that's no big deal. 你必须编写IndexOfAny代码,但这没什么大不了的。 But here's my question - why bother to do it client side? 但这是我的问题 - 为什么懒得去做客户端呢? Why not call back to the server and execute the code all in one place? 为什么不回调服务器并在一个地方执行代码? I do a lot of stuff like this. 我做了很多像这样的事情。 Check out the following link: 查看以下链接:
http://aspalliance.com/1922 http://aspalliance.com/1922
It explains a way to bind, client-side, to server-side methods. 它解释了一种将客户端绑定到服务器端方法的方法。
Although this is an old question, this is a problem I face frequently. 虽然这是一个老问题,但这是我经常遇到的问题。 I therefore wrote a tutorial on how to solve it. 因此,我写了一个如何解决它的教程。 It's located here: http://nicoschuele.com/Posts/75.html 它位于: http : //nicoschuele.com/Posts/75.html
The short answer is this: first, you need to treat all the diacritical characters within a function and then, using a dictionnary you build, you need to process all the language specific letters. 简短的回答是:首先,您需要处理函数中的所有变音字符,然后,使用您构建的字典,您需要处理所有特定于语言的字母。 For example, "à" is a diacritical character and "Ø" is a norwegian letter. 例如,“à”是变音字符,“Ø”是挪威字母。 My tutorial uses .NET to achieve this but the principle, even in javascript, is the same. 我的教程使用.NET来实现这一点,但即使在javascript中,原理也是一样的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.