1

Possible Duplicate:
How to ignore acute accent in a javascript regex match?

I have some javascript as :

var myString = 'préposition_preposition';
var regex = new RegExp("epo", "ig");
alert(myString.match(regex));

is it possible to match "épo" and "epo", if I set in regex only epo (or only épo)?

Community
  • 1
  • 1
  • Follow the link. It is exactly the same question as yours, and there are useful answers. To avoid duplicates on this website, we "close as duplicate" if possible :) – Florian Margaine Nov 02 '12 at 10:12
  • Is it an option to strip the accents from the original string entirely before trying the regex match? http://lehelk.com/2011/05/06/script-to-remove-diacritics/ – Ian Roberts Nov 02 '12 at 10:12
  • thank you, I will try implement my proper method with this exemple –  Nov 02 '12 at 10:31
  • Why not try replace? `alert(myString.replace('é', 'e'))` – KingRider Apr 22 '16 at 14:13
  • See my answer here: https://stackoverflow.com/a/74122020/1202385 ... you can remove diacritics characters on the string to match instead. – vcarel Oct 19 '22 at 08:16

3 Answers3

1

I had the same problem recently. Regex operates with ascii, therefor special characters like é or ß are not recognized. You need to explicitely include those into your regex.

Use this:

var regex = /[ée]po/gi;

Hint: Don't use new Regex() it's rather slow, but declare the regex directly instead. This also solves some quoting/escaping issues.

Christoph
  • 50,121
  • 21
  • 99
  • 128
  • problem is that I have two large text (french, english) I search most standard method to ignore accent, execute matching and show results within lose original text –  Nov 02 '12 at 10:13
  • Well, you either need to replace those accents, (take a look [at this answer](http://stackoverflow.com/a/228006/1047823)) or you always have to declare the character-classes manually, like in my answer. – Christoph Nov 02 '12 at 10:16
0

No you can not achieve this behavior. RegEx match exactly the string you provided. How should the computer know when épo or epo is what you are looking for!

But you can specify a class of chracters that can be matched new RegExp("[eé]po", "ig");

clentfort
  • 2,454
  • 17
  • 19
-3

Try this:

var str = 'préposition_preposition';
str.match(/(e|é)po/gi);
Minko Gechev
  • 25,304
  • 9
  • 61
  • 68