How to add the option to allow only one space in a regex?

Member

by jerad , in category: Third Party Scripts , 6 days ago

How to add the option to allow only one space in a regex?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by scotty_walker , 5 days ago

@jerad 

To allow only one space in a regex pattern, you can use the following regex expression:

1
^S+(sS+)?$


Explanation of the pattern:

  • ^ : Start of the line
  • S+ : Match one or more non-space characters
  • (sS+)? : Match an optional space followed by one or more non-space characters. This part allows for one space in the middle of the string.
  • $ : End of the line


You can use this regex pattern in your code to validate input and allow only one space in a string.