簡體   English   中英

如何使用JavaScript將非英語字符轉換為英語

[英]How to Convert Non-English Characters to English Using JavaScript

我有一個ac#函數,它將所有非英文字符轉換為給定文本的正確字符。 如下

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?

這應該是你正在尋找的 - 檢查演示測試。

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

當前演示

編輯:更新了要添加的演示以測試輸入。

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

轉換它當然是可能的......

ToLower - > toLowerCase,Replace => replace,Length => length

你必須編寫IndexOfAny代碼,但這沒什么大不了的。 但這是我的問題 - 為什么懶得去做客戶端呢? 為什么不回調服務器並在一個地方執行代碼? 我做了很多像這樣的事情。 查看以下鏈接:

http://aspalliance.com/1922

它解釋了一種將客戶端綁定到服務器端方法的方法。

雖然這是一個老問題,但這是我經常遇到的問題。 因此,我寫了一個如何解決它的教程。 它位於: http//nicoschuele.com/Posts/75.html

簡短的回答是:首先,您需要處理函數中的所有變音字符,然后,使用您構建的字典,您需要處理所有特定於語言的字母。 例如,“à”是變音字符,“Ø”是挪威字母。 我的教程使用.NET來實現這一點,但即使在javascript中,原理也是一樣的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM