4

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[] { 'ş','ç','ö','ğ','ü','ı'});
            //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. is it possible to convert above function to js ?

Raj
  • 22,346
  • 14
  • 99
  • 142
Adam Right
  • 955
  • 6
  • 17
  • 35

4 Answers4

7

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

Current Demo

Edit: Updated the demo to add for testing of input.

Rion Williams
  • 74,820
  • 37
  • 200
  • 327
  • 1
    hi rionmonster, thanks for the code. and yes it raises error both on [returnString = returnString.replace(/[\s-]+/, " ").trim();] and [returnString = returnString.substring(0, returnString.length <= maxLength ? returnString.Length : maxLength).trim();] i think there is a syntax error .. but it gave me hints... – Adam Right Jan 06 '11 at 19:24
  • I changed the "returnString = returnString.substring(0, returnString.length <= maxLength ? returnString.Length : maxLength).trim();" into the if statement as that should accomplish the same thing. You can mess with the demo if you'd like, as I don't really have sample input to test with. Hope this helps. – Rion Williams Jan 06 '11 at 19:27
  • 1
    trim() is not a native JS string function. – ken Jan 06 '11 at 19:36
  • Thanks Ken - it's easy to miss while using jsFiddle. – Rion Williams Jan 06 '11 at 19:41
  • my friends, Rionmonster function works without the trim(), but it converts the first non-english letter. for example: "öçşıöçşi" => "ocsiöçşı" – Adam Right Jan 06 '11 at 19:54
  • Rionmonster , i need change // add hyphens part. it converts just first blank space to hypens. it should be "returnString = returnString.replace(/\s/g, "-");". as you did for //Convert Characters part .. thanks again.. :) – Adam Right Jan 06 '11 at 20:13
  • Doh! - Sorry Adam, I missed that one :) I'll change it in the appropriate spots. – Rion Williams Jan 07 '11 at 14:30
  • hımm, i've noticed a problem. on safari browser replace() function does not work properly. "ş" and "ı" chars goes 'undifened'. but it works on IE. is there a common function which works on all browsers and can be used instead of replace() ? – Adam Right Jan 07 '11 at 21:31
  • another sample for safari error : word "şişme" is converted to "undefinediundefinedme". But same phrase is converted to "sisme" on both IE and firefox as expected :) – Adam Right Jan 07 '11 at 22:01
  • That's a bit wild - I'll have to look into that. – Rion Williams Jan 08 '11 at 05:27
3
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;
}
ken
  • 3,650
  • 1
  • 30
  • 43
  • hi ken , thanks for the code. but i am getting "Microsoft JScript runtime error: Object doesn't support this property or method" error on " if (str.test(rx)) { " line. – Adam Right Jan 06 '11 at 19:59
1

It's certainly possible to convert it...

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

You'd have to code up IndexOfAny, but that's no big deal. 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

It explains a way to bind, client-side, to server-side methods.

Chris B. Behrens
  • 6,255
  • 8
  • 45
  • 71
  • Hi Chris, thanks for your comment and help. my boss does not want it on call back process, just client side with js. i am looking into RegEx documents to implement it to js.. – Adam Right Jan 06 '11 at 19:10
-1

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

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.

Bart
  • 19,692
  • 7
  • 68
  • 77
ilovebigmacs
  • 983
  • 16
  • 28