【问题标题】:How to Convert Non-English Characters to English Using JavaScript如何使用 JavaScript 将非英文字符转换为英文
【发布时间】: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


【解决方案1】:

这应该是您正在寻找的 - 检查演示以进行测试。

   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);
}

Current Demo

编辑:更新了演示以添加测试输入。

【讨论】:

  • 嗨 rionmonster,感谢您的代码。是的,它在 [returnString = returnString.replace(/[\s-]+/, " ").trim();] 和 [returnString = returnString.substring(0, returnString.length
  • 我改变了“returnString = returnString.substring(0, returnString.length
  • trim() 不是原生 JS 字符串函数。
  • 感谢 Ken - 使用 jsFiddle 时很容易错过。
  • 我的朋友们,Rionmonster 函数可以在没有 trim() 的情况下工作,但它会转换第一个非英文字母。例如:“öçşıöçşi” => “ocsiöçşı”
【解决方案2】:
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;
}

【讨论】:

  • 嗨,谢谢你的代码。但我在“ if (str.test(rx)) { ”行上收到“Microsoft JScript 运行时错误:对象不支持此属性或方法”错误。
【解决方案3】:

当然可以转换...

ToLower -> toLowerCase,替换 => 替换,长度 => 长度

您必须编写 IndexOfAny,但这没什么大不了的。但这是我的问题 - 为什么要费心去做客户端?为什么不回调服务器并在一个地方执行代码呢?我做了很多这样的事情。查看以下链接:

http://aspalliance.com/1922

它解释了一种将客户端绑定到服务器端方法的方法。

【讨论】:

  • 嗨,克里斯,感谢您的评论和帮助。我的老板不希望它在回调过程中,只是客户端与 js。我正在研究 RegEx 文档以将其实施到 js..
【解决方案4】:

虽然这是一个老问题,但这是我经常遇到的问题。因此,我写了一篇关于如何解决它的教程。它位于此处:http://nicoschuele.com/Posts/75.html

简短的回答是:首先,您需要处理函数中的所有变音字符,然后,使用您构建的字典,您需要处理所有语言特定的字母。例如,“à”是一个变音字符,“Ø”是一个挪威字母。我的教程使用 .NET 来实现这一点,但原理,即使在 javascript 中也是一样的。

【讨论】:

    猜你喜欢
    • 2016-10-08
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 2018-09-14
    相关资源
    最近更新 更多