最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex expression that matches if the string contain only spaces? - Stack Overflow

programmeradmin1浏览0评论

I am using formik with yup to validate a form. In one input it passes if it contains only spaces, which is not valid for my use-case. So I went through Google to find some kind of regex so if the string only has spaces it would throw some message, but I didn't find anything.

nome: string()
      .min(3, '* Deve conter no mínimo 3 caracteres')
      .required('* Este campo é obrigatório')
      .matches(/[^\s*].*[^\s*]/g, '* This field cannot contain only blankspaces'),

The problem with this is that when it reaches 3 characters even with required yup condition the validation doesn't work, and matches with regex don't block the spaces. So far I managed a work-around with: nome: values.nome.trim().replace(/\s+/g, ' ') But with the correct regex expression I can throw the error in real time.

I am using formik with yup to validate a form. In one input it passes if it contains only spaces, which is not valid for my use-case. So I went through Google to find some kind of regex so if the string only has spaces it would throw some message, but I didn't find anything.

nome: string()
      .min(3, '* Deve conter no mínimo 3 caracteres')
      .required('* Este campo é obrigatório')
      .matches(/[^\s*].*[^\s*]/g, '* This field cannot contain only blankspaces'),

The problem with this is that when it reaches 3 characters even with required yup condition the validation doesn't work, and matches with regex don't block the spaces. So far I managed a work-around with: nome: values.nome.trim().replace(/\s+/g, ' ') But with the correct regex expression I can throw the error in real time.

Share Improve this question edited May 13, 2020 at 18:20 Jon Mitten 2,0554 gold badges29 silver badges57 bronze badges asked May 13, 2020 at 17:38 cristiano soarescristiano soares 831 gold badge2 silver badges6 bronze badges 7
  • Try /^\S+$/ if you need to match a whole string that has no whitespace – Wiktor Stribiżew Commented May 13, 2020 at 17:42
  • Please provide an example input / output – Pitto Commented May 13, 2020 at 17:42
  • Some kinda of : space.next === someCaracter ? valid : notValid logic and this "space next" would be looked by some regex express – cristiano soares Commented May 13, 2020 at 17:42
  • Are you just making sure that the field does not contany any white space? In this case why not simply matchin for \s+ ? – Pitto Commented May 13, 2020 at 17:43
  • @WiktorStribiżew it worked if the whole string has no whitespace but if I wanna some kinda of example: fullname= Alex martines ? – cristiano soares Commented May 13, 2020 at 18:05
 |  Show 2 more comments

3 Answers 3

Reset to default 10

You may use

/^(?!\s+$).*/
/^(?!\s+$)/
/^\s*\S.*$/
/^\s*\S[^]*$/
/^\s*\S[\s\S]*$/

The first two regexps will match any string that is not equal to 1 or more whitespaces.

The last third, fourth and fifth match a string that contains at least one non-whitespace char.

See the regex demo

Details

  • ^ - start of string
  • (?!\s+$) - a negative lookahead that fails the match if there are 1 or more whitespaces and end of string immediately to the right of the current location
  • .* - 0 or more chars other than line break chars, as many as possible
  • \s*\S.* - zero or more whitespaces, a non-whitespace, and then any zero or more chars other than line break chars
  • \s*\S[^]* / \s*\S[\s\S]* - same as above but any chars including line breaks can appear after the non-whitespace char
  • $ - end of string

Since you want to match in case any white space is present please try:

nome: string()
      .min(3, '* Deve conter no mínimo 3 caracteres')
      .required('* Este campo é obrigatório')
      .matches(/^(\S+$)/g, '* This field cannot contain only blankspaces'),

.matches(/^(\S+$)/, '* This field cannot contain only blankspaces') worked for me as well.

Mark the capital S but not the small s

发布评论

评论列表(0)

  1. 暂无评论