I have already found similar question but unfortunately it does not fit to my requirements -> regular expression for anything but an empty string
Question: Is this possible to match with Regex anything or nothing but not string which contains only whitespaces?
String s1 = ""; // -> true
String s2 = "test"; // -> true
String s3 = " test"; // -> true
String s5 = " "; // -> false
String s6 = " "; // -> false
CodePudding user response:
You can use
^(?!\s $).*
See the regex demo (changed to PCRE and \h
to match just horizontal whitespaces for the sake of the demo (\h
is not supported in JavaScript regex)).
See a JavaScript snippet showcasing how the regex works:
const trues = ["", "test", " test"];
const falses = [" ", "\t"];
const regex = /^(?!\s $).*/;
console.log(trues.every(x => regex.test(x))); // true, all true cases are matched
console.log(falses.every(x => !regex.test(x))); // true, all false cases failed to match
CodePudding user response:
This can solve your issues:
/\s/g