最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regular Expression to allow french text as well as english text? - Stack Overflow

programmeradmin4浏览0评论

I want to use a regular expression which will allow

  1. English text which does not have a special character.
  2. French Text which does not have a special character.

It will always disallow special characters like @, #, % etc... in both the language.

I have tried with the below code:

if (this.value.match(/[^a-zA-Z0-9 ]/g)) {
    this.value = this.value.replace(/[^a-zA-Z0-9 ]/g, '');
}

It works fine with english text, but the problem is when I provide a french text like éléphant, it considers the french characters as special character, and deletes the french characters. so éléphant becomes lphant.

Is there any way to allow the french characters inside the regular expression?

Thanks a lot in advance.

I want to use a regular expression which will allow

  1. English text which does not have a special character.
  2. French Text which does not have a special character.

It will always disallow special characters like @, #, % etc... in both the language.

I have tried with the below code:

if (this.value.match(/[^a-zA-Z0-9 ]/g)) {
    this.value = this.value.replace(/[^a-zA-Z0-9 ]/g, '');
}

It works fine with english text, but the problem is when I provide a french text like éléphant, it considers the french characters as special character, and deletes the french characters. so éléphant becomes lphant.

Is there any way to allow the french characters inside the regular expression?

Thanks a lot in advance.

Share Improve this question edited Oct 29, 2013 at 7:38 Suvankar Bhattacharya asked Oct 29, 2013 at 7:31 Suvankar BhattacharyaSuvankar Bhattacharya 1291 gold badge2 silver badges9 bronze badges 2
  • possible duplicate of Matching accented characters with Javascript regexes – Cristian Lupascu Commented Oct 29, 2013 at 7:38
  • 1 a nice resource for this.... – Wrikken Commented Oct 29, 2013 at 7:52
Add a comment  | 

3 Answers 3

Reset to default 14

Quick solution:

/[^a-zA-Z0-9 àâäèéêëîïôœùûüÿçÀÂÄÈÉÊËÎÏÔŒÙÛÜŸÇ]/

Reference: List of french characters

Hope this helps

Most simplified solution:

/[^a-zA-ZÀ-ÿ]/  

(or)

/[\wÀ-ÿ]/       // Note: This will allow "_" also

Any of the above regular expression will work in your case.

I would suggest normalizing string before replacing chars.

This example is a JAVA normalization, but maybe this example could help you with javascript

    String string = "éléphante";

    string = Normalizer.normalize(string, Normalizer.Form.NFD);

    string = string.replaceAll("[^\\p{ASCII}]", "");

    System.out.println(string.replaceAll("[^a-zA-Z0-9 ]", ""));
发布评论

评论列表(0)

  1. 暂无评论