PHP preg_match needed to ensure only ONE space character is allowed between words

后端 未结 3 1755
广开言路
广开言路 2020-12-11 23:47

basically I need a preg_match that ensures that only one space character is allowed between each word (IF there is more than one word).

My existing rule is:

相关标签:
3条回答
  • 2020-12-12 00:11

    It depends what you mean by "word", but assuming you mean letters in A-Z or a-z you can try this:

    ^[a-zA-Z]+( [a-zA-Z]+)*$
    

    Note that \s does not mean the space character - it means any whitespace, including a new line.

    0 讨论(0)
  • 2020-12-12 00:18
    /^[\S]+\s?[^\s]?/i
    

    Will search for any word with one space after it and not a space after that one space

    /\s?\S+\s/
    

    Will search for any character with whitespace on either side or only on the end.

    0 讨论(0)
  • 2020-12-12 00:30

    i think to specify a number of occurences to your match you put i.e{1} {1,5} (to match only 1 or between 1 and 5 inclusive) so something like...

    ^[A-Za-z]+( {1})$

    0 讨论(0)
提交回复
热议问题