-1

I`m building messaging app and I need regex or some other way to validate message that user is about to send. Message must contain at least one character, expect for space.

const valid = *regex*

const message_a = "Hello, Jim!";
const message_b = " ";
const message_c = "      ";

valid.test(message_a) === **true**
valid.test(message_b) === **false**
valid.test(message_c) === **false**

I don't know how to make regex, so I just searched for such regex and found none. So I'm really looking forward for your answers, thanks!

1 Answers1

1

If you want to go the regex route, simply match on \S:

var input = "Hello, Jim!";
if (/\S/.test(input)) {
    console.log("MATCH");
}

The pattern /\S/ will match any single non whitespace character, anywhere in the input.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360