How do I test for no whitespaces in a string, using regex? I'm using JQuery
var postcode = $(this), val = postcode.val();
if(val.test(NO WHITESPACE)){
...
}
Any help is appreciated, Thanks
How do I test for no whitespaces in a string, using regex? I'm using JQuery
var postcode = $(this), val = postcode.val();
if(val.test(NO WHITESPACE)){
...
}
Any help is appreciated, Thanks
if (/\s/.test(string)) alert("OH NO THERE IS FILTHY WHITESPACE IN THAT STRING");
The "\s" ... uhh, thing, in a regex means "any whitespace character". Specifically, it means the same as this:
[ \f\n\r\t\v\u00A0\u2028\u2029]
which is to say, space, form feed, line feed, carriage return, tab, vertical tab, and some space-like characters from extended Latin and Unicode.
If it's only spaces, you don't need a RegExp: no spaces = val.indexOf(' ') < 0
val.match(/\s/g) === nullUse the following code this would replace all the blank spaces..
string.replace(/^\s+|\s+$/g,'')
In fact it would remove even newline characters
The spaces are actually removed by \s
all the white spaces but only those at the begining and at the end of the string. And more, this doesn't answer the question.