I'm trying to create a validation rules for username in two steps:
- Detect if strings contains any non latin characters. All non albhabetic symbols/numbers/whitespaces are allowed.
- Detect if string contains any symbols which are not in the whitelist (' - _ `). All latin/non latin characters/numbers/whitespaces are allowed.
I thought it would be easy, but I was wrong...
- For the first case I've tried to remove latin characters/numbers/whitespaces from the string:
str.replace(/[A-Za-z0-9\s]/g, '')
With such rule from "Xxx z 88A ююю 4$??!!" I will get "ююю$??!!". But how to remove all symbols ("ююю" should stay)?
- For the second case I've tried to remove latin characters/numbers/whitespaces/symbols from whitelist(' - _ `) with str.replace(/[A-Za-z0-9-_`\s]/g, ''), but I don't know hot to remove non latin characters.
Summary: My main problem is to detect non latin characters and separate them from special symbols.
UPDATE: Ok, for my second case I can use:
str.replace(/[\u0250-\ue007]/g, '').replace(/[A-Za-z0-9-_`\s]/g, '')
It works, but looks dirty... Pardon for backticks.