Domanda

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.

È stato utile?

Soluzione

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

Altri suggerimenti

You have to use the XRegexp plugin, along with the Unicode add-on.

Once you have that, you can use modern regexes like /[\p{L}\p{Nl}]/, which necessarily also includes those \p{Greek} code points which are letters or letter-numbers. But you could also match /[\p{Latin}\p{Greek}]/ if you wanted.

Javascript’s own regexes are terrible. Use XRegexp.

So something like: /^[^?&\^"]*$/ (that means the string is composed only of characters outside the five you listed)...

But if you want to have the greek characters and the unicode characters (what are unicode characters? àèéìòù? Japanese?) perhaps you'll have to use http://xregexp.com/ It is a regex library for javascript that includes character classes for the various unicode character classes (I know I'm repeating myself) plus other "commands" for unicode handling.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top