Regex - allow only single spaces within a string

I need to limit the string to allow letters, numbers, hyphens, ampersands, apostrophes, and single spaces.

From a small search I still have this:

^[A-Za-z0-9-'&\s]{1,}$ 

But this allows for double space. How to write a regular expression so that it allows only single spaces (maybe there wasn’t at all)?

+4
source share
2 answers

Matches any other permitted value, followed by additional space:

 ^\s?([A-Za-z0-9-'&]\s?){1,}$ 

(I also added an option at the beginning, if allowed)

+7
source
 Try this ^([A-Za-z0-9-'&]+\s?)+$ 
+1
source

Source: https://habr.com/ru/post/1496125/


All Articles