Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to allow all alphabet characters plus unicode characters

I need a regular expression to allow all alphabet characters plus Greek/German alphabet in a string but replace those symbols ?,&,^,". with *

I skipped the list with characters to escape to made the question simple. I really want to see how to construct this and afterwards include alphabet sets using ASCII codes.

like image 441
Panos Kal. Avatar asked Jan 17 '23 05:01

Panos Kal.


1 Answers

if you have a finite and short set of elements to replace you could just use a class e.g.

 string.replace(/[?\^&]/g, '*');

and add as many symbols as you want to reject. you could also add ranges of unicode symbols you want to replace (e.g. \u017F-\036F\u0400-\uFFFF )

otherwise use a a class to specify what symbols don't need to be replaced, like a-z, accented/diacritic letters and greek symbols

 string.replace(/[^a-z\00C0-\017E\u0370-\03FF]/gi, '*');
like image 64
Fabrizio Calderan Avatar answered Jan 31 '23 09:01

Fabrizio Calderan