0

I have the following code

https://stackblitz.com/edit/angular-uvxifq-qrjtpg

pattern="^\s+$"

I need to negate this, because as of now I got error when I put a letter, but it should only show error IF there is only white space.

I tried using ?! but it shows an error

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
YoloSwaggg
  • 71
  • 1
  • 7

2 Answers2

0

If you want to use a negative lookahead (?!, you could check if from the start till the end of the string there are no whitespace characters:

pattern="^(?!\s*$)[\s\S]+$"

Regex demo

That will match

  • ^ Start of string
  • (?!\s*$) Negative lookahead, assert that what follows is not 0+ whitespace characters and end of string
  • [\s\S]+ Match 1+ times any character including new lines
  • $ End of string
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

You can use the following regex:

^(?: *[^\s] *)+$

demo: https://regex101.com/r/th0A3A/3/

Allan
  • 12,117
  • 3
  • 27
  • 51