-1

How to check in JavaScript if the specified character is not a whitespace using regex only? Right now I am doing something like the code below with negation ! but I would like to avoid mixing of two things to avoid confusions.

if (!/\s/.test(character))
    console.log('this is not a whitespace');
no one special
  • 1,608
  • 13
  • 32

2 Answers2

4
if (/\S/.test(character))
    console.log('this is not a whitespace');
patstuart
  • 1,931
  • 1
  • 19
  • 29
2

Use the negated set notation in the regex

/[^\s]/

That will match everything that isnt a whitespace.

diopside
  • 2,981
  • 11
  • 23