[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Replace Accent Char

Posted on 2006-04-24
6
Medium Priority
?
657 Views
Last Modified: 2008-01-09
Hi experts,

I need to replace all the accent from a string:

é->e è->e à->a

I need a very compact script.

0
Comment
Question by:cabou
6 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 16523493
Like this:

var newStr = oldStr.replace(/[éè]/g,"e").replace(/[à]/g,"a");

0
 
LVL 8

Expert Comment

by:KennyTM
ID: 16524424
function replace_accent_chars (str) {
    function rac_analyzer (x) {
            if (x < 'ß')
                  return rac_analyzer(x.toLowerCase()).toUpperCase();
            else {
                  switch (x) {
                        case 'à': case 'á': case 'â': case 'ã': case 'ä': case 'å':
                              return 'a';
                        case 'æ':
                              return 'ae';
                        case 'ç':
                              return 'c';
                        case 'è': case 'é': case 'ê': case 'ë':
                              return 'e';
                        case 'ì': case 'í': case 'î': case 'ï':
                              return 'i';
                        case 'ð':
                              return 'd';
                        case 'ñ':
                              return 'n';
                        case 'ò': case 'ó': case 'ô': case 'õ': case 'ö': case 'ø':
                              return 'o';
                        case 'ù': case 'ú': case 'û': case 'ü':
                              return 'u';
                        case 'ý': case 'ÿ':
                              return 'y';
                        case 'þ':
                              return 'th';
                        case 'ß':
                              return 'ss';
                        default:
                              alert('à'.charCodeAt(0));
                              return x;
                  }
            }
    }
   
    return str.replace(/[À-ÿ]/g, rac_analyzer);
}

alert (replace_accent_chars ("Ampère's law and l'Hôpital's rule") );
0
 
LVL 8

Accepted Solution

by:
KennyTM earned 2000 total points
ID: 16524429
// Replacing all accented characters in ISO-8859-1 range.

function replace_accent_chars (str) {
    function rac_analyzer (x) {
            if (x < 'ß')
                  return rac_analyzer(x.toLowerCase()).toUpperCase();
            else {
                  switch (x) {
                        case 'à': case 'á': case 'â': case 'ã': case 'ä': case 'å':
                              return 'a';
                        case 'æ':
                              return 'ae';
                        case 'ç':
                              return 'c';
                        case 'è': case 'é': case 'ê': case 'ë':
                              return 'e';
                        case 'ì': case 'í': case 'î': case 'ï':
                              return 'i';
                        case 'ð':
                              return 'd';
                        case 'ñ':
                              return 'n';
                        case 'ò': case 'ó': case 'ô': case 'õ': case 'ö': case 'ø':
                              return 'o';
                        case 'ù': case 'ú': case 'û': case 'ü':
                              return 'u';
                        case 'ý': case 'ÿ':
                              return 'y';
                        case 'þ':
                              return 'th';
                        case 'ß':
                              return 'ss';
                        default:
                              return x;
                  }
            }
    }
   
    return str.replace(/[À-ÿ]/g, rac_analyzer);
}

alert (replace_accent_chars ("Ampère's law and l'Hôpital's rule") );
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:cabou
ID: 16524677
Kenny,

I like your code but when I runs it into my page the Special Char desepears...
What do I have to do?

You can see it at:
http://beta.a7fr.com
0
 
LVL 15

Expert Comment

by:dbritt
ID: 16524743
You need to save it in a different encoding.
0
 
LVL 8

Expert Comment

by:KennyTM
ID: 16524834
I cannot see any "Special Chars" (those <?>'s ?), but, as dbritt has said, this may be an encoding problem.

Make sure the page is in UTF-8...
Try adding this into the <head> section:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question