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

Member

by jerad , in category: Third Party Scripts , 4 months ago

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

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by scotty_walker , 4 months 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.

Related Threads:

How to allow only 1 forward slash in regex expression?
How to ignore white space in string using regex?
How to capture values with a space after hyphen with regex?
How to allow multiple spaces in between the text using regex?
How to add recursive option to mocha programmatically?
How to add custom option to git command?