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

2
  • It should not accept only spaces right? Commented Jan 8, 2019 at 7:57
  • empty or only space should return the error. If I write anything like 100space and a letter, this should be ok Commented Jan 8, 2019 at 7:58

2 Answers 2

0

You can use the following regex:

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

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.