How to write a regular expression not missing a space at the beginning and at the end and skipping a single space in the middle? Thank you in advance!

  • one
    What characters are allowed to the right and left of the space? - cheops
  • Do not skip a space at the beginning of a line and at the end of a line, skip only one space in the middle - AbylaiM
  • Is it a name? Only letters are allowed or can it use numbers, punctuation marks? - cheops
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

4 answers 4

maybe something like that

^[^ ]([^ ]| (?!.+ .+))+[^ ]$ 

demo

  • $ .validator.addMethod ("has_space", function (value, element) {return! (/ ^ \ s + \ s + $ /). test (value);}, "Please enter a valid input."); This piece of code at the beginning and at the end does not skip the gap now how does the code allow one gap in the middle? - AbylaiM
  • Your regular expression will not miss the text of 1 or 2 literals: ( - ReinRaus
  • @ReinRaus yes, you are right - at least 3. - splash58

A regular expression can be broken down into 3 conditions:

  1. Must not begin with a space
  2. Should not end with a space
  3. There should not be two spaces in the text.

We make positional checks for each of the three conditions:

  1. ^(?! )
  2. (?!.* $)
  3. (?!(?:.* ){2})

In addition to these conditions, allow all other literals in the string:

 ^.*$ 

and combine everything into one regular expression:

 /^(?! )(?!.* $)(?!(?:.* ){2}).*$/ 

https://regex101.com/r/fK9pA3/1


Formally, an empty line fits the described conditions, but most likely for practical tasks there should be at least 1 character in the text, which means we add one more rule:

  1. At least one character in the text.

Solve it like this: ^.+$

 /^(?! )(?!.* $)(?!(?:.* ){2}).+$/ 

https://regex101.com/r/fK9pA3/2

  • The regular expression is based on the method described here.stackoverflow.com/a/450416/481 (unfortunately I don’t know where to find an authoritative source for this method). - ReinRaus
  • class hour check - AbylaiM

It seems possible to direct:

 /^[^ ]+( [^ ]+)?$/ 

1. ^[^ ]+ - at the beginning of the line, some count of non-whitespace characters. 2. ( [^ ]+)?$ - followed by a possible group of spaces and a number of non-blank characters.

  • Th didn’t work out - AbylaiM
  • may not have explained so - AbylaiM
  • do not allow space at the beginning of a line and at the end of a line. And skip only one space in the middle of a line - AbylaiM
  • And what did not work out? > var name = 'John Doe'; undefined > name.match(/^[^ ]+( [^ ]+)?$/) [ 'John Doe', ' Doe', index: 0, input: 'John Doe' ] - tonal
  • problem not solved (((((((((( - - AbylaiM

You can do the following:

 var name = 'John Doe'; if(name.match(/^\w+ \w+$/i)) console.log('Success'); else console.log('Fail'); 
  • and for jquery plugin how to write a regular expression - AbylaiM
  • @cheops I understand that the space may not be - splash58
  • only one space in the middle - AbylaiM