How to Convert Non-English Characters to English Using JavaScript

Posted by Adam Right on Stack Overflow See other posts from Stack Overflow or by Adam Right
Published on 2011-01-06T18:35:38Z Indexed on 2011/01/06 20:53 UTC
Read the original article Hit count: 476

Filed under:
|
|

I have a c# function which converts all non-english characters to proper characters for a given text. like as follows

public static string convertString(string phrase)
        {
            int maxLength = 100;
            string str = phrase.ToLower();
            int i = str.IndexOfAny( new char[] { 's','ç','ö','g','ü','i'});
            //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', 's');
                outPut.Replace('i', 'i');
                outPut.Replace('g', '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. is it possible to convert above function to js ?

waiting all kinds of suggestion.

thanks in advance..

© Stack Overflow or respective owner

Related posts about c#

Related posts about JavaScript