RegEx expression not allowing only spaces? -


i have regex expression allows spaces, letters , dashes. i'd modify wouldn't allow spaces too. can me ?

/^([a-zăâîșțĂÂÎȘȚ-\s])+$/ 

you can use negative lookahead restrict generic pattern:

/^(?!\s+$)[a-za-zăâîșțĂÂÎȘȚ\s-]+$/   ^^^^^^^^ 

see regex demo

the (?!\s+$) lookahead executed once @ beginning , returns false if there 1 or more whitespaces until end of string.

also, regex contained classical issue of [a-z] matches more ascii letters, need replace [a-za-z] (or [a-z] , use /i case insensitive modifier).

also, - inside character class placed @ end not escape it, , parsed literal hyphen (however, might want escape if developer have update pattern adding more symbols character class).

and in case regex engine not support lookarounds:

^[a-za-zăâîșțĂÂÎȘȚ\s-]*[a-za-zăâîșțĂÂÎȘȚ-][a-za-zăâîșțĂÂÎȘȚ\s-]*$ 

it requires @ least 1 non-space character allowed set (also matching 1 obligatory symbol).

another regex demo


Comments