javascriptregexunicodecharacter-properties

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.


Solution

  • 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, '*');