0

I am trying to match a regex criteria in json schema validation where a string should not accept blank spaces if entire string consists of blank spaces or blank spaces are between any input, but should accept if there are any blank spaces before and after characters, numbers or any special characters

Say

str = "  " 

should not be accepted

or

str = "ab cd" 

should not be accepted. But

str = "abcd  " 

should be accepted or

str = "  abcd" 

should be accepted.

I have used below regex pattern

"pattern":"^[^\\s]*$"

But this is not accepting any blank spaces in the string. All the above scenarios mentioned is showing invalid.

0

1 Answer 1

3

You may use

"pattern":"^\\s*\\S+\\s*$"

See the regex demo

Details

  • ^ - start of string
  • \s* - 0+ whitespaces
  • \S+ - 1+ non-whitespace chars
  • \s* - 0+ whitespaces
  • $ - end of string.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.